Macro Stops Working After First Run

rob51852

Board Regular
Joined
Jun 27, 2016
Messages
190
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I am trying to put together a macro that finds a name in a column header then copies that column if the header matches.

It works the first time I run it but when the pasted column is deleted and the macro is run again I get a Run-time Error 2004 Application-defined or Object-defined error on this line:

VBA Code:
Set sRange = Sheets("Data").Range("C1", Cells(1, LastCol))

Full code:

VBA Code:
Sub Copy()

Dim Cell As Range, sRange As Range, Rng As Range

    LastCol = Sheets("Data").Cells(1, Columns.Count).End(xlToLeft).Column

        Set sRange = Sheets("Data").Range("C1", Cells(1, LastCol))

            With sRange

                Set Rng = .Find(What:="Chennai", _
                                After:=.Cells(1), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious, _
                                MatchCase:=False)

                    If Not Rng Is Nothing Then

                        Lastrow = Sheets("Data").Cells(Rows.Count, Rng.Column).End(xlUp).Row

                            Sheets("Data").Range(Rng, Cells(Lastrow, Rng.Column)).Copy _
                                Destination:=Sheets("Summary").Range("A7")
                    End If
            End With
End Sub

Can anyone see the issue?

Thanks
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
That code would only work if Data is the active sheet. It should be written as:

Code:
       Set sRange = Sheets("Data").Range("C1", Sheets("Data").Cells(1, LastCol))
 
Upvote 0
Also, you should be familiar with the forum rules on cross-posting by now, so please follow them in future. Thanks. :)
 
Upvote 0
Thanks Rory. This works perfectly (and point taken).
 
Upvote 0
Glad to help. :)
 
Upvote 0

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

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