【发布时间】:2016-03-31 23:25:17
【问题描述】:
我是 VBA 的新手,但是我被赋予了使用 VBA 完成的任务。如何创建一个代码,通过将完全相同数量的单独工作表添加到此主数据文件中,从不同工作簿复制多个工作表的数据并将它们粘贴到另一个工作簿(主数据文件)?也就是说,我想在主数据文件中显示所有这些工作表被复制到单独的工作表中。
我已经设法提取了一个代码,该代码将数据复制并粘贴到一个工作表中,但我正在努力将它们一一复制到单独的工作表中。
非常感谢您的帮助。
Sub datatransfer()
Dim FolderPath, FilePath, Filename, targetfile As String
Dim wb1, wb2 As Workbook
Dim i, mycount As Long
targetfile = "Left the location out on purpose"
FolderPath = " Left the location out on purpose "
FilePath = FolderPath & "*.xls*"
Filename = Dir(FilePath)
Dim lastrow, lastcolumn As Long
Do While Filename < ""
mycount = mycount + 1
Filename = Dir()
Set wb1 = Workbooks.Open(FolderPath & Filename)
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
Set wb2 = Workbooks.Open(targetfile)
Worksheets.Add Before:=Sheet1, Count:=2
For i = 1 To mycount
With Worksheets(i)
ActiveSheet.Paste Destination:=.Range(Cells(2, 2), Cells(2, lastcolumn))
End With
Next i
ActiveWorkbook.Close SaveChanges:=True
Filename = Dir
Loop
End Sub
【问题讨论】: