我在同一文件夹中有四个Excel工作簿:JAN18
,FEB18
,MAR18
和manWrkbk
。
我想循环从工作簿D46
,E15
和B12
和JAN18
分别从相同的单元格获取值FEB18
并依次显示在MAR18
中,如下所示。
如何使用VBA代码完成此操作?
答案 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