从2个工作簿复制到1个工作簿

时间:2020-04-11 19:19:02

标签: excel vba

获取

运行时错误1004

当试图从两个不同的工作簿复制到同一工作簿时

我要将某些单元格从两个工作表复制到一个工作表中。我尝试了几种其他方法来获取其他错误,所以我回到了这一方法。

Sub Range_Copy_Examples()

Dim wb As Workbook
Dim wb1 As Workbook
Dim wbData As Range
Dim wbData1 As Range
Dim wbExtract As Range
Dim wbExtract1 As Range

    Set wb = ActiveWorkbook
    Set wbData = wb.Worksheets("GAI").Range("A1")
    Set wbExtract = wb.Worksheets("Report").Range("A3:I3")

    wbData.CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wbExtract

    Set wb1 = ActiveWorkbook
    Set wbData1 = wb1.Worksheets("FUND").Range("H1")
    Set wbExtract1 = wb.Worksheets("Report").Range("J3:K3")

    wbData1.CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wbExtract1

End Sub

1 个答案:

答案 0 :(得分:0)

这对设置为ActiweWorkbook的两个工作簿均无效,请在您存储此代码的一个工作簿上尝试将名称更改为ThisWorkbook,并用您要从中复制另一个工作簿的扩展名填充该名称。

Sub Range_Copy_Examples()

Dim wb As Workbook
Dim wb1 As Workbook
Dim wbData As Range
Dim wbData1 As Range
Dim wbExtract As Range
Dim wbExtract1 As Range

Set wb = ThisWorkbook
Set wbData = wb.Worksheets("GAI").Range("A1")
Set wbExtract = wb.Worksheets("Report").Range("A3:I3")

wbData.CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wbExtract

Set wb1 = Workbooks("TheOtherWorkbook.xls")
Set wbData1 = wb1.Worksheets("FUND").Range("H1")
Set wbExtract1 = wb.Worksheets("Report").Range("J3:K3")

wbData1.CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wbExtract1

结束子