【问题标题】:Excel: Extract columns from one workbook to append to another workbookExcel:从一个工作簿中提取列以附加到另一个工作簿
【发布时间】:2011-07-11 07:27:31
【问题描述】:

我有这样的问题: 我有多个工作簿与数据共享相同的模板:(假设它们被命名为“1.xlsx”、“2.xlsx”......) A2:文件#(例如:在 ""1.xlsx" 中是 "File1") Field1 Field2 Field3 Field4(存储在D3:G40中的数据)

我有一个文件名“Full.xlsx” 文件# 字段 1 字段 2

现在我需要将数据 D3:E40 从“1.xlsx”、“2.xlsx”...复制到“Full.xlsx”(Field1、Field2)的 sheet1,并将它们的 A2 转到 File#

例如,我有:

1.xlsx:
[A2] File1
[D2]Field1 [E2]Field2 [F2]Field3 [G2]Field4
[D3]aa [E3]bb [F3]cc [G3]dd
[D4]ee [E4]ff [F4]gg [G4]hh
...

2.xlsx:
[A2] File2
Field1 Field2 Field3 Field4
11 22 33 44
55 66 77 88
...

I need to have sheet1 in "Full.xlsx":

[A1]File# [B1]Filed1 [C1]Filed2
[A2]File1 [B2]aa [C2]bb
[A3]File1 [B3]ee [C3]ff
...
[A40]File2 [B40]11 [C40]22
[A41]File2 [B41]55 [C41]66
...

有人能告诉我怎么做吗? (我使用的是 MS Excel 2010)

更新:我终于写出了一些有用的东西(我是 VBA 新手,所以它可能不是最好的解决方案)

Public Sub copyrows()
Dim FileNum As String
Dim LastRow As Long, i As Long, Counter As Integer
Dim Dest As Workbook
Set Dest = Workbooks("Full.xlsm")

FileNum = Cells(2, 1).Value

Dest.Activate

LastRow = Dest.Worksheets("Sheet1").Range("C65536").End(xlUp).Row + 1
i = LastRow

For Counter = 3 To 40
    Dest.Worksheets("Sheet1").Cells(i, 1).Value = FileNum
    i = i + 1
Next

ThisWorkbook.Activate
ThisWorkbook.Worksheets("Sheet1").Range("D3", "E40").Copy
Dest.Activate
Dest.Worksheets("Sheet1").Range("B" & LastRow, "C" & i).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
End Sub

感谢大家的关心!

【问题讨论】:

  • 你想通过程序来做吗???或者你想像 Excel 公式一样做同样的事情?请说明
  • @Sumit 问题用 VBA 标记。
  • @Sumit:无论哪种方式都可以。我只需要完成工作。如果你知道,请告诉我。

标签: excel vba


【解决方案1】:

您可以使用搜索框查看这里发生的情况: - How can I copy columns from one sheet to another with VBA in Excel? - Copy data from one sheet to another using VBA - Excel Copy Worksheet from external WorkBook

顺便说一句,您可以在当前页面的右栏中找到许多相关帖子。

但是,如果您需要帮助,请发布您已经构建的代码,以便我们帮助您改进或调试它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    相关资源
    最近更新 更多