Copy range from one workbook to another

insomniac_ut

New Member
Joined
Sep 15, 2005
Messages
36
Hello,

I'm trying to copy a fixed dimension range from one workbook to the end of a worksheet in another workbook.

This is the code I'm using to achieve this, though it's not particularly stable. I occasionally get a UsedRange of Object _Worksheet Failed or PasteSpecial Method of Range Class Failed error.

Is there something wrong with this code, or can someone suggest some improvements?

Thank you.

Code:
Public Sub CopyRanges()

    Dim wbSource As Workbook
    Dim wbDest As Workbook
    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim LastRowFinder As Long
    
    Application.CutCopyMode = False
    Application.ScreenUpdating = False
    
    Set wbSource = Workbooks.Open("C:\Users\xyz\Desktop\Source.xls")
    Set wsSource = wbSource.Worksheets("Sheet2")
    
    Set wbDest = Workbooks.Open("C:\Users\xyz\Desktop\Target.xls")
    Set wsDest = wbDest.Worksheets("Sheet2")
    
    wsSource.Range("B2:F40").Copy
    
    LastRowFinder = wsDest.UsedRange.Rows.Count
    wsDest.Range("B" & LastRowFinder + 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    
    Application.CutCopyMode = False
    
    wbDest.Save
    wbDest.Close
    
    wbSource.Save
    wbSource.Close
    
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Have you checked that using UsedRange to find the last row us actually giving the correct result?

Also, is the destination worksheet/ workbook protected?

If you are calling this code from outside Excel have you tried it from Excel?
 
Upvote 0
Hi Norie,

1. UsedRange is managing to find the last row correctly
2. The destination workbook is not protected
3. That's just it - it sometimes throws errors from outside Excel. To right this, I open up Excel and run the macro (it runs correctly). I then save the sheet and try calling it from outside Excel and no problems.

This error is very sporadic/random. Out of curiosity, in my declarations should I be using New Workbook/Worksheet instead of just Workbook/Worksheet?

Thanks.
 
Upvote 0
Can I ask how you are checking the last row is correct each time?

Are you debugging the code, perhaps stepping through it, adding watches for all the variables etc?

Oh, and no you shouldn't be using New.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,754
Members
452,940
Latest member
rootytrip

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