如何遍历各种工作簿以将特定的单元格复制到另一个工作簿中?

时间:2018-10-06 06:46:56

标签: excel vba

我在同一文件夹中有四个Excel工作簿:JAN18FEB18MAR18manWrkbk

我想循环从工作簿D46E15B12JAN18分别从相同的单元格获取值FEB18并依次显示在MAR18中,如下所示。

如何使用VBA代码完成此操作?

Expected result

1 个答案:

答案 0 :(得分:0)

Private Sub CopyWb()
Dim Filename As String, path As String
path = ThisWorkbook.path & "\"
Filename = Dir(path)

Do Until Filename = ""
If Filename = ThisWorkbook.Name Then
GoTo Last1
End If
Workbooks.Open path & Filename
Cells(46, 4).Copy
ActiveWorkbook.Close

ThisWorkbook.Activate

Range("A1048576").End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll

Last1:
Filename = Dir()
Loop

path = ThisWorkbook.path & "\"
Filename = Dir(path)

Do Until Filename = ""
If Filename = ThisWorkbook.Name Then
GoTo Last2
End If
Workbooks.Open path & Filename
Cells(15, 5).Copy
ActiveWorkbook.Close

ThisWorkbook.Activate

Range("B1048576").End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll

Last2:
Filename = Dir()
Loop

path = ThisWorkbook.path & "\"
Filename = Dir(path)

Do Until Filename = ""
If Filename = ThisWorkbook.Name Then
GoTo Last3
End If
Workbooks.Open path & Filename
Cells(12, 2).Copy
ActiveWorkbook.Close

ThisWorkbook.Activate

Range("C1048576").End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
Range("D1048576").End(xlUp).Offset(1, 0).Value = Filename
Last3:
Filename = Dir()
Loop
End Sub