我已经在Access 2010中编写了一些VBA代码来清理包含不需要的AutoRun宏的Excel 4.0工作簿 - 因为我只对数据感兴趣,所以将特定工作表复制到新Excel似乎是个好主意相反,2010年的工作手册。
但是,当VBA进入.Copy
语句时,我收到错误1004 Method 'Copy' of object '_Worksheet' failed
。
这不是带有多个副本的MS错误,或者在复制之前没有保存等等 - 这是试图复制到现有的工作簿中,虽然这个错误代码在这里很流行,但我找不到方法和/或对我有用的答案......
代码中有问题的部分是:
'declarations
Dim varFile As Variant
Dim varNewFile As Variant
Dim xlApp As Excel.Application
Dim xlWbk As Excel.Workbook
Dim xlWb2 As Excel.Workbook
Dim xlSht As Excel.Worksheet
Dim xlSh2 As Object 'will be a member of the Sheets collection (not worksheets)
'filenames - constPath is a constant defining the pathname to the folder
varFile = constPath & "\site.xls"
varNewFile = constPath & "\sitenew.xlsx"
'create instance of Excel, open source workbook and point to worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlWbk = GetObject(varFile)
Set xlSht = xlWbk.Worksheets("sites")
'open destination workbook (already exists)
Set xlWb2 = GetObject(varNewFile)
'rename worksheet with same name
For Each xlSh2 In xlWb2.Sheets
If xlSh2.Name = "sites" Then
xlSh2.Name = "sitesOLD"
End If
Next xlSh2
'this next statement is where it fails
'copy across worksheet into existing workbook
xlSht.Copy Before:=xlWb2.Sheets(1)
我已经检查了源数据,并且在长度方面似乎没有单元格值的问题(它的名称和地址信息,加上一些是/否指示符)。
答案 0 :(得分:0)
这可能有很多原因。将xlApp.xlSht.Copy放在哪里工作?