I want to copy data from one already open 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 a "1004" runtime error: Failed to execute copy method of Range class
How to copy data from one already open instance of Excel to another instance of Excel in VBA?
I understand how to do this when they are part of the same instance. In this particular case, I need the two books to be in separate copies. I also need to make a full copy (checking data, formulas, values, formats, etc.), Therefore r2.Value = r1.Value will not be sufficient.
source
share