Run time error

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
574
Office Version
  1. 365
Platform
  1. Windows
I am trying to build a macro that will copy the cells from C2 to the bottom of the column across several sheets and paste them into one column on the first worksheet. When I run the macro below, it crashes on the "set rng..." line with the error, "Run-time error '1004': Method 'Range' of object'_Worksheet' failed". Can someone help me figure out what I am missing with this one?

Thanks for the help,

Robert


Code:
Sub Extract()


Dim ws As Worksheet
Dim rng As Range


For Each ws In ActiveWorkbook.Worksheets
'If ws.Name <> "Combined" Then


    If ws.Name <> "Combined" Then
        Set rng = ws.Range("C2" & ws.Rows.Count).End(xlUp)
        'Storing first copied data in A2
        If IsEmpty(Sheets("Combined").[A2]) Then
            rng.Copy Sheets("Combined").Range("A2" & Rows.Count).End(xlUp)
        'Storing next copied data below previously filled cell
        Else
            rng.Copy Sheets("Combined").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
        End If
    End If
Next ws


End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try
Code:
        Set rng = ws.Range("C2", ws.Range("C" & ws.Rows.Count).End(xlUp))
 
Upvote 0
Awesome! Thanks for the fix. The next line failed with the same error, but I used your fix on the first line and incorporated it into the next line of code and it worked. Thanks again.
 
Upvote 0
Glad to help & thanks for the feedback

If by next line, you mean this line
Code:
rng.Copy Sheets("Combined").Range("A2" & Rows.Count).End(xlUp)
I think it should be
Code:
rng.Copy Sheets("Combined").Range("A2")
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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