当我尝试将工作表上的工作表复制到另一个工作表时,我总是收到超出范围的错误。原始电子表格(Master_Data.xlsm)是运行vba脚本的内容。脚本打开另一个电子表格,对其进行处理,然后复制要粘贴到Master_Data.xlsm工作簿中的最终工作表。
Sub Result_Scrapper()
Dim wb As Workbook, ws As Worksheet, wbFile As Object
Dim masterBook As Workbook
Dim wsa As Worksheet
Dim year As Integer
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder("C:\Users\Output_Spreadsheets\")
Set masterBook = Excel.Workbooks("Master_Data.xlsm")
Application.ScreenUpdating = False
For Each wbFile In fldr.Files
If fso.GetExtensionName(wbFile.Name) = "xlsm" Then
Set wb = Workbooks.Open(wbFile.Path)
'Copy sheet of interest
ActiveSheet.Copy 'Before:=ThisWorkbook.Sheets(“A”) 'tried doing it using before statement but it also caused errors
'paste sheet into masterBook spread--this is where the error comes
masterBook.Sheets(Sheets.Count).Paste
End If
masterBook.Sheets("master").Name = Right([A2], 30)
Next wbFile
答案 0 :(得分:2)
有两个问题。首先,正如其他人所述,您需要完全限定该计数。其次,您需要一行完成。并且您可以这样做,但是随后您只需推出最后一张纸就可以了,如果您在之后添加它,那么这些纸将保持顺序。
尝试:
ActiveSheet.Copy After:=masterBook.Sheets(masterBook.Sheets.Count)