Excel文件复制导致VBA错误(运行时错误424:对象必需)

因此,我正在开发一个程序来获取数据“.txt”格式,并读取某些值,然后将这些值放在外部报表中,并将其格式化为我公司使用的标准文档。 然后复制已格式化的文档,并将其保存在单独的工作簿中,这些工作簿将成为格式化工作表的集体数据库,供以后审阅或打印出来。

显然,我已经愚蠢的代码只是胆量问题发生的地方,但它仍然代表了我的基本想法是什么,并导致相同的警报作为较大的文件。

Sub Begin() Workbooks.Open Filename:="C:\Users\CNCLathe.MANDA\Desktop\XL\COPYING\Report1.xlsx" Workbooks.Open Filename:="C:\Users\CNCLathe.MANDA\Desktop\XL\COPYING\Report2.xlsx" '--------------------------------------------------------------------------------- 'The File With the Sub Program organizing information Set W1 = Workbooks("TEST.xlsm") 'The File With the Formatted Worksheet 'Changes will not be saved when it closes Set W2 = Workbooks("Report1.xlsx") 'The File with all the new formatted worksheets added to it Set W3 = Workbooks("Report2.xlsx") '--------------------------------------------------------------------------------- 'pretending to add data to formatted file as I will later W2.Sheets("Sheet1").Cells(2, 2).Value = 999 'then I need to copy the formated worksheet[as values] 'and add it to Report2 'Error Occurs here W2.Sheets("Sheet1").Copy After:=W1.Sheets(wb.Sheets.Count) 'ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value Workbooks("Report1.xlsx").Activate MsgBox ("OPEN") 'ActiveWorkbook.Save ActiveWorkbook.Close SaveChanges:=False End Sub 

wb在哪里定义? 也许而不是wb.Sheets.Count ,请尝试W1.Sheets.Count