Copying Data from One Workbook to Another

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have 2 workbooks, one named BR1VOA and the other BR1 New Vehicles

I set up a macro below to copy the data from Br1VOA1.xls to BR1 New Vehicles.xls


The macro works well where there is data in BR1VOA1, but if there is no data, i.e all the cells are blank, it comes up with run time error 1004, 'The information cannot be pasted because the copy area & paste area are not the same area & shape"

It would be appreciated if you could assist




Sub Copy_Data()

Windows("Br1VOA.xls").Activate
Range(Cells(2, "a"), Cells(2, "a").End(xlDown).Offset(, 2)).Copy
Windows("BR1 New Vehicles.xls").Activate
Cells(6, "a").PasteSpecial
Range("A1").Select
With Range("A:A")
.HorizontalAlignment = xlLeft
End With


Howard
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi Howard

Since you are copying from row 2, you could first check what the last row of data is in column A (this assumes that every record in the sheet will have a populated value in column A):

Code:
Dim lngLastRow As Long
With Workbooks("Br1VOA.xls").WorkSheets(1)
  lngLastRow = .Cells(.Rows.Count,"A").End(xlUp).Row
  If lngLastRow > 1 And lngLastRow < 65530 Then
    .Range("A2:A" &  lngLastRow).Offset(0,2).Copy Destination:= _
     Workbooks("BR1 New Vehicles.xls").Worksheets(1).Range("A6")
  Else
    MsgBox "Range out of bounds!"
  End If
End With
Workbooks("BR1 New Vehicles.xls").Worksheets(1).Range("A6:A" & lngLastRow+6-1).HorizontalAlignment = xlLeft
 
Upvote 0
Hi Richard

Thanks for the help. Code works perfectly

Regards

Howard
 
Upvote 0
Hi Richard

I have just checked your code on Another bookbook rovvoa1)where the data in Column A to C from row 2 onwards in supposed to be copied in cell A6 onwards on Workbook Rov VOA's.Newvehicles.xls

Only the data from C2 onwards is copied into the destination workbook

See Source & Destination Workbook

Your assistance will be most appreciated

Regards

Howard


Source Workbook
rovvoa1.xls
ABCD
1StockNoSupplyToBalance
2467659MRKOSEI-AMANKWAH690
3474876MRLFOSSATI7977.17
4752432MRNGODENDAAL20533.62
5
Nissvoa1
Rov VOA's.New Vehicles.xls
ABCD
1RovNewVehicleVOA'sasat07/07/2008
2
3
4
5StockNo.CustomerAmountDate
6690
77977.17
820533.62
9
Sheet1
 
Upvote 0
Hi Howard

Apologies - I misinterpreted your original code. Amend to:

Code:
Dim lngLastRow As Long
With Workbooks("Br1VOA.xls").WorkSheets(1)
  lngLastRow = .Cells(.Rows.Count,"A").End(xlUp).Row
  If lngLastRow > 1 And lngLastRow < 65530 Then
    .Range("A2:A" &  lngLastRow).Resize(,3).Copy Destination:= _
     Workbooks("BR1 New Vehicles.xls").Worksheets(1).Range("A6")
  Else
    MsgBox "Range out of bounds!"
  End If
End With
Workbooks("BR1 New Vehicles.xls").Worksheets(1).Range("A6:A" & lngLastRow+6-1).HorizontalAlignment = xlLeft
 
Upvote 0
Hi Richard

Thanks for the help

Much appreciated


Howard
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top