Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Range between two instances of excel

Tags:

excel

vba

I'm running two separate instances of Excel and I'm trying to copy data from a Range in one workbook to the other.

I have this code:

Sub CopyValues()
Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
Dim Src As Range
Dim Dst As Range
    Set Src = xlApp.ActiveSheet.Range("A1:A9")
    Set Dst = Workbooks("Book1.xlsm").Worksheets("Sheet1").Range("A1:A9")
    Src.Copy Dst
End Sub

It doesn't return any errors but it doesn't copy the values, Also tried this for the last line

Src.Value = Dst.Value

Still does nothing

My VBA skills are not so good, just started learning it 2 weeks ago.

like image 777
Ovidiu Nacov Avatar asked Dec 31 '14 14:12

Ovidiu Nacov


People also ask

How do I copy a formula from one Excel file to another?

Unfortunately, there is no easy way to get formulas from one instance of Excel to another. The best you can do is the following: In the source workbook, select a single cell that you want to copy. Press F2. Excel switches to Edit mode. While still in Edit mode, select everything in the cell (the entire formula). Press Ctrl+C.

How to get formulas from one excel instance to another?

You can't even use Paste Special from the Edit menu to paste formulas; that isn't an option when working with two instances. Unfortunately, there is no easy way to get formulas from one instance of Excel to another.

How do I copy and paste between two monitors in Excel?

Within the Excel window, use the mouse to arrange the two workbooks so one is on each monitor. Since you are using a single instance of Excel—even though it extends across two monitors—you can now cut, copy, and paste as you normally would. ExcelTips is your source for cost-effective Microsoft Excel training.

Can I work with multiple Excel workbooks at the same time?

You can then work with both workbooks onscreen at the same time. One thing you'll notice, however, is that copying information from one instance of Excel to another works differently than when you copy within the same instance.


1 Answers

If you want to avoid using the clipboard, and assuming that your handle to the other instance of Excel (xlApp) is correct, then you should be able to use an array to get and set your data.

Sub CopyValues()
    Dim xlApp As Excel.Application
    Dim Src As Range
    Dim Dst As Range
    Dim Vals() as Variant

    Set xlApp = GetObject(, "Excel.Application")

    Set Src = xlApp.ActiveSheet.Range("A1:A9")
    Set Dst = Workbooks("Book1.xlsm").Worksheets("Sheet1").Range("A1:A9")

    Vals = Src
    Dst.Value = Vals
End Sub
like image 53
phrebh Avatar answered Oct 18 '22 21:10

phrebh