如何在 VBA 中的两个打开的 Excel 实例之间进行复制?

发布于 2024-12-11 14:48:08 字数 905 浏览 0 评论 0原文

我想将数据从一个已打开的 Excel 实例复制到 VBA 中的另一个 Excel 实例。我尝试过:

Option Explicit
Sub copy_paste()

    Dim destination_sanitized As String
    Dim fs As New FileSystemObject

    destination_sanitized = fs.BuildPath("c:\temp\", "1.xlsx")

    Dim xl As New Excel.Application

    Dim wb As Workbook
    Set wb = xl.Workbooks.Open(Filename:=destination_sanitized)

    Dim r1 As Range
    Dim r2 As Range
    Set r1 = ThisWorkbook.Sheets("hidden").Range("E10:E13")
    Set r2 = wb.Sheets("Sheet1").Range("J20:J23")

    On Error GoTo Cleanup
    r1.Copy r2

Cleanup:
    wb.Close SaveChanges:=False
    Set xl = Nothing
    MsgBox Err.Number & ": " & Err.description


End Sub

我收到运行时错误“1004”:Range 类的复制方法失败

如何将数据从一个已打开的 Excel 实例复制到 VBA 中的另一个 Excel 实例?

我知道当它们属于同一实例时如何执行此操作。在这种特殊情况下,我需要将两个工作簿放在不同的实例中。我还需要做一个完整的副本(数据验证、公式、值、格式等),所以 r2.Value = r1.Value 是不够的。

I want to copy data from one already opened instance of Excel to another instance of Excel in VBA. I tried:

Option Explicit
Sub copy_paste()

    Dim destination_sanitized As String
    Dim fs As New FileSystemObject

    destination_sanitized = fs.BuildPath("c:\temp\", "1.xlsx")

    Dim xl As New Excel.Application

    Dim wb As Workbook
    Set wb = xl.Workbooks.Open(Filename:=destination_sanitized)

    Dim r1 As Range
    Dim r2 As Range
    Set r1 = ThisWorkbook.Sheets("hidden").Range("E10:E13")
    Set r2 = wb.Sheets("Sheet1").Range("J20:J23")

    On Error GoTo Cleanup
    r1.Copy r2

Cleanup:
    wb.Close SaveChanges:=False
    Set xl = Nothing
    MsgBox Err.Number & ": " & Err.description


End Sub

I get Run-time error '1004': Copy method of Range class failed

How can I copy data from one already opened instance of Excel to another Excel instance in VBA?

I understand how to do this when they are part of the same instance. In this particular case, I need the two workbooks to be in separate instances. I also need to do a full copy (Data Validation, Formulas, Values, Formats, etc), so r2.Value = r1.Value will not suffice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿看小鸭子会跑 2024-12-18 14:48:09

我认为您需要详细说明为什么需要单独的实例,到目前为止,在我的职业生涯中,我从未有任何理由在 Excel 中使用单独的实例,这对于自动化来说是一场噩梦。

话虽这么说,您可以尝试这样的操作(假设您只打开了 2 个实例):

Sub MM()

    Dim varTask As Variant
    Dim XL1 As Application, XL2 As Application
    Dim r1 As Range, r2 As Range
    Dim OtherWB As Workbook
    Dim destination_sanitized As String

    destination_sanitized = CreateObject("Scripting.FileSystemObject").BuildPath("C:\temp\", "1.xlsx")

    With CreateObject("Word.Application")
       If .Tasks.Exists("Microsoft Excel") Then
           For Each varTask In .Tasks
           Debug.Print varTask
                 If InStr(varTask.Name, "Microsoft Excel") = 1 Then
                      If XL1 Is Nothing Then
                        Set XL1 = GetObject(Replace(varTask, "Microsoft Excel - ", "")).Application
                      Else
                        Set XL2 = GetObject(Replace(varTask, "Microsoft Excel - ", "")).Application
                      End If
                 End If
           Next varTask
       End If
       .Quit
    End With

    'Then something like...

    Set r1 = ThisWorkbook.Sheets("hidden").Range("E10:E13")
    Set OtherWB = XL2.Workbooks.Open(destination_sanitized)
    Set r2 = OtherWB.Sheets("Sheet1").Range("J20:J23")
    r1.Copy r2

    'Clear down memory afterwards
    Set r1 = Nothing
    Set r2 = Nothing
    OtherWB.Close False
    Set OtherWB = Nothing
    Set XL1 = Nothing
    XL2.Quit
    Set XL2 = Nothing

End Sub

I think you need to elaborate as to why you need separate instances, so far in my career I have never had any reason to use a separate instance in Excel and it is a nightmare for automation.

That being said, you can give something like this a try (assuming you only have 2 instances open):

Sub MM()

    Dim varTask As Variant
    Dim XL1 As Application, XL2 As Application
    Dim r1 As Range, r2 As Range
    Dim OtherWB As Workbook
    Dim destination_sanitized As String

    destination_sanitized = CreateObject("Scripting.FileSystemObject").BuildPath("C:\temp\", "1.xlsx")

    With CreateObject("Word.Application")
       If .Tasks.Exists("Microsoft Excel") Then
           For Each varTask In .Tasks
           Debug.Print varTask
                 If InStr(varTask.Name, "Microsoft Excel") = 1 Then
                      If XL1 Is Nothing Then
                        Set XL1 = GetObject(Replace(varTask, "Microsoft Excel - ", "")).Application
                      Else
                        Set XL2 = GetObject(Replace(varTask, "Microsoft Excel - ", "")).Application
                      End If
                 End If
           Next varTask
       End If
       .Quit
    End With

    'Then something like...

    Set r1 = ThisWorkbook.Sheets("hidden").Range("E10:E13")
    Set OtherWB = XL2.Workbooks.Open(destination_sanitized)
    Set r2 = OtherWB.Sheets("Sheet1").Range("J20:J23")
    r1.Copy r2

    'Clear down memory afterwards
    Set r1 = Nothing
    Set r2 = Nothing
    OtherWB.Close False
    Set OtherWB = Nothing
    Set XL1 = Nothing
    XL2.Quit
    Set XL2 = Nothing

End Sub
沫尐诺 2024-12-18 14:48:08

让两个 Excel 实例在所有情况下都能相互通信是很困难的。您可以找到其他正在运行的实例,但要考虑的事情太多了。

在类似的情况下,我会保持简单并制作两个按钮:

  • 导出,保存到clipboard.csv或clipboard.xlsx或其他内容
    格式化您想要复制的数据
  • 导入,从剪贴板临时文件获取数据

用户负责单击一个实例上的“导出”按钮,然后单击第二个实例上的“导入”按钮。

It is difficult to get two instances of Excel to talk to each other in all situations. You can find the other running instances, but there are too many things to consider.

In similar cases I keep it simple and make two buttons:

  • Export that saves to clipboard.csv or clipboard.xlsx or whatever
    format you like the data to copy
  • Import that gets the data from the clipboard temporary file

The user is in charge of clicking on the Export button on one instance, then on the Import button on the second instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文