要在单独的Excel实例之间复制和粘贴的VBA代码

时间:2018-01-14 06:08:02

标签: excel vba excel-vba

我正在尝试从excel中的一个打开实例复制数据并将其加载到单独的打开实例中。我有以下代码,但它只复制自上次保存以来源工作簿中的数据。此代码也只能从目标工作簿运行。任何帮助将不胜感激。

Sub CollectA()

Dim oApp As Application
Dim oWb As Workbook
Set oWb = GetObject("Test two.xlsm")
Set oApp = oWb.Parent
oWb.Activate
oWb.ActiveSheet.Range("A1").Select
Selection.Copy


Workbooks("Test three.xlsm").Worksheets("Sheet1").Range("B1").PasteSpecial  Paste:=xlPasteValues


End Sub

1 个答案:

答案 0 :(得分:1)

尽可能避免复制/粘贴:

Sub CollectA()

    Dim oWb As Workbook
    Set oWb = GetObject("Test two.xlsm")

    Workbooks("Test three.xlsm").Worksheets("Sheet1").Range("B1").Value = oWb.ActiveSheet.Range("A1").Value

End Sub

如果您希望宏位于“测试二”中:

Sub CollectA()

    Dim oWb As Workbook
    Set oWb = GetObject("Test three.xlsm")

    owb.Worksheets("Sheet1").Range("B1").Value = Workbooks("Test two.xlsm").ActiveSheet.Range("A1").Value

End Sub