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.
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