Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB: Copy a Range to another Excel Application

Tags:

range

excel

vba

I'm trying to solve a relatively simple problem but I can't realize it. My aim is to copy a range of cells in a worksheet of the main Excel Application to another range (of the same size) in a worksheet of a second newly created Excel Application. I create the second Application by using

Set secondExApp = CreateObject("Excel.Application")

I'm using this reference for further handling. Until now I've tried two different ways. Both don't work properly.

0.: Preparation / Introduction

Set srcWb = Application.ActiveWorkbook
Set srcSheet = srcWb.Worksheets("example")

Set dstApp = CreateObject("Excel.Application")
Set dstWb = dstApp.Workbooks(1)
Set dstSheet = dstWb.Worksheets(1)

1.: PasteSpecial - delivers an image(!) instead of just the range

srcSheet.Range("A1:B2").Copy
dstSheet.Range("A1:B2").PasteSpecial xlPasteAll

2.: Range.Copy [Destination] - does not work - Is it right that I can only use this method for sheets in the same application?

srcSheet.Range(srcSheet.Cells(..., ...), srcSheet.Cells(..., ...)).Copy _
dstSheet.Range(dstSheet.Cells(..., ...), dstSheet.Cells(..., ...))

Any help is appreciated.


Edit: I've already played with the "record macro" functionality but I prefer coding it on my own without "selecting" or "activating" cells / sheets / etc.


Edit (solved): Thank you both GSerg and iDevlop very much, you delivered a good further starting point for me. I did some research as far as the Excel constants as xlClipboardFormatDspText are concerned.

What really helped me was the fact that opening a new Excel instance changes the Paste(Special) menu.

So instead of creating a new instance I now simply add a workbook (which can be hidden) and use this object to add my content. Since it is held in the same instance (also have a look at the task manager) the Paste(Special) menu is completely the same.

Now it is possible to use Range.Copy [destination] even without select!

Result:

'Hides the new workbook
Application.ScreenUpdating = False

Set dstWb = Workbooks.Add
Set dstSheet = dstWb.Worksheets(1)

srcSheet.Range(srcSheet.Cells(..., ...), srcSheet.Cells(..., ...)).Copy
dstSheet.Paste dstSheet.Range(dstSheet.Cells(..., ...), dstSheet.Cells(..., ...))

'Avoids the often seen dashed border around the copied range
Application.CutCopyMode = False

'Setting the initial change back
Application.ScreenUpdating = True

Software: Excel 2007

like image 997
Mic Avatar asked Oct 10 '22 04:10

Mic


1 Answers

As determined by poking Excel with a stick, you have to use Worksheet.Paste for inter-excel stuff:

srcSheet.Range("A1:B2").Copy
dstSheet.Paste dstSheet.Range("A1")

Poking Excel with a thicker stick revealed that formulas get preserved when pasting from Clipboard as xlClipboardFormatDspText:

srcSheet.Range("A1:B2").Copy
dstSheet.Range("A1").Select
dstSheet.PasteSpecial xlClipboardFormatDspText, False

However, this does require selecting a cell on dstSheet first, because Worksheet.PasteSpecial uses active cell.

like image 168
GSerg Avatar answered Oct 13 '22 10:10

GSerg