PDA

View Full Version : Solved: Copy a range from 1 workbook to another workbook



Audax
04-03-2007, 09:36 AM
Hi experts

I want to copy Workbook1-Sheet3-Range C8:C18 to Workbook2-Sheet5-Range H15:H25

Workbook1
Sheet3 - Range from C8:C18

Workbook2
Sheet5 - Range from H15:H25

Thank you

mdmackillop
04-03-2007, 09:49 AM
Have you tried using the macro recorder to do this?

Audax
04-03-2007, 10:55 AM
Thank you mdmackillop for your answer, but I tried that and I receive the following error...That command cannot be used on multiples selections.

I gave an simple example, but I want to copy multiples selections see the example below.

Workbook1
Sheet3 - Range from C8:C18...Range C23-C47...Range C52-C64

TO

Workbook2
Sheet5 - Range from H15:H25..Range H29-H53..Range H68-H80

Any VBA can solve this?

Thank you for your help

lucas
04-03-2007, 11:10 AM
This might work Audax. Put it in a standard module in Workbook2 and make sure that workbook1 is in the same directory. You can change the path to workbook1 in the code:
Sub GetDataFromClosedWorkbook()
Dim wb As Workbook
Application.ScreenUpdating = False ' turn off the screen updating

'Make path selections below
' Set wb = Workbooks.Open("f:\Temp\Social Club.xls", True, True)
Set wb = Workbooks.Open(ActiveWorkbook.Path & "\Workbook1.xls")
' the sheet in this workbook to copy to
With ThisWorkbook.Worksheets("Sheet5")
' read data from the source workbook
'the range to copy to in this workbook-name of sheet to copy FROM-range in closed workbook to copy
.Range("H15", "H25").Formula = wb.Worksheets("Sheet3").Range("C8", "C18").Formula
.Range("H29", "H53").Formula = wb.Worksheets("Sheet3").Range("C23", "C47").Formula
.Range("H68", "H80").Formula = wb.Worksheets("Sheet3").Range("C52", "C64").Formula
End With
wb.Close False ' close the source workbook without saving any changes
Set wb = Nothing ' free memory
Application.ScreenUpdating = True ' turn on the screen updating
End Sub

Audax
04-03-2007, 12:00 PM
Thanks Lucas, but it doesn't work, maybe I'm doing something wrong, I'm not very good with VBA, any other suggestion?

Thanks a lot

lucas
04-03-2007, 12:23 PM
Maybe if you could post your workbook. Zip it up and hit post reply and then scroll down till you find manage attachments. Doesn't have to be the actual data, just a good example.

lucas
04-03-2007, 12:25 PM
remember this opens the closed workbook1 and names of workbooks and worksheets have to be changed to match yours....what error do you get exactly?

Audax
04-03-2007, 12:40 PM
Thank again...Let try to explain a little better Using the same example above, I have 2 Workbooks (Workbook1 and Workbook2) both are opened, I want to copy from Workbook1

Sheet3 - Range from C8:C18...Range C23-C47...Range C52-C64

TO Workbook2

Sheet5 - Range from H15:H25..Range H29-H53..Range H68-H80

Thank you for your patience

lucas
04-03-2007, 12:56 PM
read the comments closely...they will tell you what you need to change. See attached...open Workbook2 and click the button. Both files should be in the same directory.

Audax
04-04-2007, 11:47 AM
Thanks Lucas I've tested your example and works 100%, but my problem still continue, I'll try to figure out for myself.

Thank a lot again

lucas
04-04-2007, 12:03 PM
merged cells can cause problems....avoid them at all costs.....
If you can't figure it out maybe post some example of what your trying to do so we can look at it.