Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VBA中的两个打开的Excel实例之间进行复制?_Vba_Excel - Fatal编程技术网

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

如何在VBA中的两个打开的Excel实例之间进行复制?,vba,excel,Vba,Excel,我想在VBA中将数据从一个已打开的Excel实例复制到另一个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.Applicati

我想在VBA中将数据从一个已打开的Excel实例复制到另一个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是不够的。

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

也就是说,您可以尝试一下这样的方法(假设您只打开了两个实例):


在所有情况下,很难让两个Excel实例彼此对话。你可以找到其他运行的实例,但是有太多的事情要考虑。 在类似情况下,我保持简单,并制作两个按钮:

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

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

所说的“但我至少同时使用10个Excel”是指在一个Excel实例中打开10个工作簿,还是选择10个单独的实例?问题解决后,接受一些答案,而不是尝试建议一些代码。建议的答案中没有一个真正解决了问题。问题在于在应用程序实例之间复制和粘贴。建议的答案是在同一应用程序实例中打开的不同工作簿之间进行复制。@yu_ominae我不同意-我的答案实际上是在实例之间进行复制,而不是在实例之间进行复制workbooks@MacroMan你在我的评论后发表了你的回答吗?我看不到历史,但上面说的是2014年的答案。
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