Run-time error '1004': Method 'Range' of object_worksheet failed

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
Hello, for some reason Excel doesn't like when I try to use the "range method" in my code below.

I would appreciate any insight as I need this to be a dynamic range.

This line doesn't work:
Code:
targetSheet.Range(Cells(1, 1), Cells(Source_Lastrow, Source_LastColumn)).Value = sourceSheet.Range(Cells(1, 1), Cells(Source_Lastrow, Source_LastColumn)).Value

But if I change it to this line, it does work:
Code:
targetSheet.Range("A1", "F9").Value = sourceSheet.Range("A1", "F9").Value

Code:
Sub TBD()
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim Source_LastColumn As Integer
Dim Source_Lastrow As Integer
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' assume range is A1 - C10 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets("2 - Insert Data")
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
'STEP 2: Identify the last column and last row with data on the active worksheet
Source_LastColumn = customerWorkbook.Sheets(1).UsedRange.Columns.Count
Source_Lastrow = customerWorkbook.Sheets(1).UsedRange.Rows.Count
targetSheet.Range(Cells(1, 1), Cells(Source_Lastrow, Source_LastColumn)).Value = sourceSheet.Range(Cells(1, 1), Cells(Source_Lastrow, Source_LastColumn)).Value
' Close customer workbook
customerWorkbook.Close
            
End Sub
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Are you simply trying to copy one range to another sheet?
If so, it doesn't like it when trying to post a multi-cell range, if you pick a multi-cell range as the destination. Just pick the upper left-most corner of your destination range, i.e.
Code:
targetSheet.Range(Cells(1, 1), Cells(Source_Lastrow, Source_LastColumn)).Copy Destination:=sourceSheet.Cells(1, 1)
 
Last edited:
Upvote 0
Hi Joe - thanks a bunch! It worked with one minor change(reversing sourceSheet and targetSheet).

Code:
sourceSheet.Range(Cells(1, 1), Cells(Source_Lastrow, Source_LastColumn)).Copy Destination:=targetSheet.Cells(1, 1)


One more question... is it possible to change my code that asks user to select the file to import from, to import based on a file path located at:

targetWorkbook....Sheet("1 - Workbook Details")....Cell($C$22)

Here is my current code:
Code:
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
 
Upvote 0
Thanks Joe! Here is the updated code that works!

Code:
Sub Extract_Report_Data()
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim Source_LastColumn As Integer
Dim Source_Lastrow As Integer
Dim Target_LastColumn As Integer
Dim Target_Lastrow As Integer
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
customerFilename = targetWorkbook.Sheets("1 - Workbook Details").Range("$C$22")
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' assume range is A1 - C10 in sheet1
' copy data from customer to target workbook
Dim targetSheet1 As Worksheet
Set targetSheet1 = targetWorkbook.Worksheets("2 - Report")
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
'STEP 2: Identify the last column and last row with data on the active worksheet
Source_LastColumn = customerWorkbook.Sheets(1).UsedRange.Columns.Count
Source_Lastrow = customerWorkbook.Sheets(1).UsedRange.Rows.Count
sourceSheet.Range(Cells(1, 1), Cells(Source_Lastrow, Source_LastColumn)).Copy Destination:=targetSheet1.Cells(1, 1)
' Close customer workbook
customerWorkbook.Close
            
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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