我正在尝试将工作表从一个工作簿复制到另一个工作簿

时间:2014-01-04 00:00:34

标签: arrays excel excel-vba vba

我正在尝试将几个完整的工作表从一个工作簿复制到另一个工作簿。我已经录制了一个宏来创建代码,但是当我将它实现到我的主工作簿中时,我得到了Subscript Out of Range error (runtime error ‘9’)。我对此问题的研究揭示了如何将数据从一个工作簿复制到另一个工作簿,而不是多个工作表。 这是代码。它应该复制Therm Cal Compiler.xlsm中的工作表并将它们粘贴到SN1813016 - Copy.xlsx之后的第四张表中:

Windows("Therm Cal Compiler.xlsm").Activate
Sheets(Array("Thermal Calibration", "Therm_R1_-_0uA_-25C", "Therm_R1_-_0uA_0C", _
    "Therm_R1_-_0uA_23C", "Therm_R1_-_0uA_40C", "Therm_R1_-_0uA_50C", _
    "Therm_R1_-_0uA_60C", "Therm_R1_-_0uA_65C", "Therm_R1_-_0uA_70C")).Select
Sheets(Array("Thermal Calibration", "Therm_R1_-_0uA_-25C", "Therm_R1_-_0uA_0C", _
    "Therm_R1_-_0uA_23C", "Therm_R1_-_0uA_40C", "Therm_R1_-_0uA_50C", _
    "Therm_R1_-_0uA_60C", "Therm_R1_-_0uA_65C", "Therm_R1_-_0uA_70C")).Copy Before _
    :=Workbooks("SN1813016 - Copy.xlsx").Sheets(5)

Therm Cal Compiler.xlsmSN1813016 - Copy.xlsx都已打开,第二个数组命令发生错误,

Sheets(Array("Thermal Calibration",…)).Copy Before _
    :=Workbooks("SN1813016 - Copy.xlsx").Sheets(5).

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:0)

如果目标工作簿没有5张,则会导致该错误。

您应该使用After:=代替。此外,ActivateSelect不是必需的。

试试这个

Workbooks("Therm Cal Compiler.xlsm").Worksheets(Array( _
  "Thermal Calibration", _
  "Therm_R1_-_0uA_-25C", _
  "Therm_R1_-_0uA_0C", _
  "Therm_R1_-_0uA_23C", _
  "Therm_R1_-_0uA_40C", _
  "Therm_R1_-_0uA_50C", _
  "Therm_R1_-_0uA_60C", _
  "Therm_R1_-_0uA_65C", _
  "Therm_R1_-_0uA_70C")).Copy _
  After:=Workbooks("SN1813016 - Copy.xlsx").Sheets(4)