如何从不同的行开始将列复制并粘贴到不同的工作簿中?使用 VBA

How do I Copy and paste columns into a different workbook starting at a different row? Using VBA

这是我目前的 VBA 代码:

Sub refreshActionLog()
'Dim current As Worksheet
'Dim source As Workbook
'Dim sourcesheet As Variant
Application.ScreenUpdating = False

Set current = ThisWorkbook.Worksheets("Log")
Set source = Workbooks("G:\Data\Shared\Action Logs\merged.xlsx").Worksheets("merged")

With Intersect(source.Sheets("merged").Columns("A:I"), source.UsedRange)
     .Columns("A").Copy Destination:=current.Range("A6")
     .Columns("B").Copy Destination:=current.Range("B6")
     .Columns("C").Copy Destination:=current.Range("C6")
     .Columns("D").Copy Destination:=current.Range("D6")
     .Columns("E").Copy Destination:=current.Range("E6")
     .Columns("G").Copy Destination:=current.Range("F6")
     .Columns("H").Copy Destination:=current.Range("G6")
     .Columns("H").Copy Destination:=current.Range("H6")
     .Columns("I").Copy Destination:=current.Range("I6")
End With
End Sub

我在行中收到错误 "Run time error '9': Subscript out of range":

Set source = Workbooks("G:\Data\Shared\Action 
Logs\merged.xlsx").Worksheets("merged")  

此外,'Intersect' 是从特定行开始复制和粘贴列的最佳方式。我不知道,因为 运行 时间错误,我的代码不会再继续了。

任何帮助将不胜感激。

在你做任何复制之前,打开你的merged.xlsx:

Set source = Workbooks.Open("G:\Data\Shared\Action Logs\merged.xlsx")

现在设置 sheet:

Dim sourceSheet as Worksheet: Set sourceSheet = source.Worksheets("merged")

现在可以复制了

sourceSheet.Range("A:I").Copy current.Range("A6")

我没有测试过这段代码,所以可能存在一些语法错误,但这是一般要点

Note: Don't forget to close 'source' workbook