【发布时间】:2016-06-03 03:22:13
【问题描述】:
我目前正在使用 VBA 脚本将来自多个工作表和工作簿的数据组合到一个新工作簿中。当前脚本会执行此操作,但会在目标工作簿中创建多个工作表。是否有可能只有目的地是一张纸?
Sub copydata()
Dim FolderPath As String, FilePath As String, FileName As String
FolderPath = "C:\attach\"
FilePath = FolderPath & "*.xlsx"
FileName = Dir(FilePath)
Dim erow As Long, lastrow As Long, lastcolumn As Long
'loops through directory as long as it is not blank and defines files as workbooks.
Do While FileName <> ""
Dim wb As Workbook
Set wb = Workbooks.Open(FolderPath & FileName)
'nested loop for sheets in workbooks
For counter = 3 To 9
'Sheets(“Sheet1”).Select
wb.Worksheets(counter).Activate
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
'Sheets("Sheet1").Select
Workbooks("ZMasterFile.xlsx").Worksheets(counter).Activate
erow = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1).Select
ActiveSheet.Paste
Next
wb.Close savechanges:=False
FileName = Dir
Loop
erow = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1).Select
【问题讨论】:
-
您在脚本的哪个位置创建工作表?他们原本不在那里吗?