【问题标题】:How can I copy between two open Excel instances in VBA?如何在 VBA 中的两个打开的 Excel 实例之间进行复制?
【发布时间】:2016-05-23 17:13:34
【问题描述】:

我想将数据从一个已打开的 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 是不够的。

【问题讨论】:

  • “但是我同时使用至少 10 个 Excel”是指您在一个 Excel 实例中打开了 10 个工作簿,还是打开了 10 个单独的实例?
  • 当您的问题解决后,接受一些答案,而不是我们会尝试建议一些代码
  • 所提出的答案都没有真正解决问题。问题是关于应用程序实例之间的复制和粘贴。建议的答案适用于在同一应用程序实例中打开的不同工作簿之间进行复制。
  • @yu_ominae 我不敢苟同 - 我的答案实际上是在 instances 而不是 workbooks 之间复制
  • @MacroMan 您是否在我发表评论后发表了您的答案?我看不到历史,但上面写着 2014 年的回答。

标签: vba excel


【解决方案1】:

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

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

  • Export 保存到 clipboard.csv 或 clipboard.xlsx 或其他 格式你喜欢的数据复制
  • 导入,从剪贴板临时文件中获取数据

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

【讨论】:

    【解决方案2】:

    我认为您需要详细说明为什么需要单独的实例,到目前为止,在我的职业生涯中,我从未有任何理由在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      相关资源
      最近更新 更多