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

时间:2013-08-22 21:03:59

标签: excel vba excel-vba

所以我正在开发一个程序,以获取数据“.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

1 个答案:

答案 0 :(得分:2)

wb在哪里定义?也许代替wb.Sheets.Count,尝试W1.Sheets.Count