我需要将一个电子表格的某些列复制到另一电子表格。
为此,我有一个宏:
Sub Submit()
Dim sourceColumn As Range, targetColumn As Range
Set sourceColumn =
Workbooks("Submission_Form.xlsm").Worksheets("RGSheet").Columns("A")
Set myData = Workbooks.Open("I:\Projects\...\Macros\RG.csv")
Worksheets(1).Select
Worksheets(1).Range("A1").Select
Set targetColumn = Workbooks("RG.csv").Worksheets(1).Columns("A")
sourceColumn.Copy Destination:=targetColumn
End Sub
但是,当光标击中targetColumn时,会出现错误:
Run-time error '9':
Subscript out of range
为简单起见,我将RG.csv与Submission_Form.xlsm放在同一文件夹中。所以我很困惑为什么会出错。
我还想知道RG.csv是否位于另一个目录中。
答案 0 :(得分:0)
该错误可能是因为未打开Submission_Form.xlsm(?)。您的代码看起来有点不一致。也许只需打开源和目标,然后复制列即可。像这样吗?
Option Explicit
Sub Submit()
Dim vSourceWorkbook As Workbook
Dim vDestinationWorkbook As Workbook
Set vSourceWorkbook = Workbooks.Open("Submission_Form.xlsm")
Set vDestinationWorkbook = Workbooks.Open("I:\Projects\...\Macros\RG.csv")
vSourceWorkbook.Worksheets("RGSheet").Range("A:A").Copy Destination:=vDestinationWorkbook.Worksheets(1).Range("A:A")
End Sub