使用PasteSpecial方法复制单元格时出错

时间:2013-05-20 09:13:04

标签: excel vbscript

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方法失败了。

1 个答案:

答案 0 :(得分:0)

PasteSpecial因观察到的错误而失败的原因是SaveAs操作清除了复制的数据。在SaveAs后移动PasteSpecial,然后将Copy移到PasteSpecial之前。我还建议在应用程序对象实例化后立即放置VisibleDisplayAlerts等常规配置属性。

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"