Set objExcel = createObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\deven.kamlesh.jain\Desktop\abcd.xlsx")
iRow = objworkbook.sheets("Sheet1").Usedrange.rows.Count
objworkbook.Sheets("sheet1").Range("A1:C"& iRow).Copy
objExcel.visible = true
Set Obj1 = objexcel.workbooks.add()
objExcel.displayalerts = false
Obj1.saveas("Copied abcd")
Obj1.Sheets("Sheet1").Range("A1").PasteSpecial
我正在尝试将一个文件复制到另一个文件,但是我收到一条错误消息,指出PasteSpecial
类的Range
方法失败了。
答案 0 :(得分:0)
PasteSpecial
因观察到的错误而失败的原因是SaveAs
操作清除了复制的数据。在SaveAs
后移动PasteSpecial
,然后将Copy
移到PasteSpecial
之前。我还建议在应用程序对象实例化后立即放置Visible
或DisplayAlerts
等常规配置属性。
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open _
("C:\Users\deven.kamlesh.jain\Desktop\abcd.xlsx")
Set Obj1 = objExcel.Workbooks.Add
iRow = objWorkbook.Sheets("Sheet1").UsedRange.Rows.Count
objWorkbook.Sheets("sheet1").Range("A1:C"& iRow).Copy
Obj1.Sheets("Sheet1").Range("A1").PasteSpecial
Obj1.SaveAs "Copied abcd"