Change macro from all sheets to active sheet

Andy15

Board Regular
Joined
Apr 1, 2017
Messages
56
Hi Guys,

I have a macro that inserts rows in a workbook and is applied to all worksheets, this works great but I am trying to modify it to only work on the open worksheet (i am supposing this is activesheet) but it keeps failing.

The code that is working on all the sheets is as follows

Code:
Sub InsertRows()Dim ws As Worksheet
Dim found As Long


Application.ScreenUpdating = False


For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Search" Then                  '    "Search" (this is excel name but can be changed by user and break code
            ws.Activate
            
    found = ws.Columns(2).Find(What:="*", After:=ws.Cells(1, 2), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
    If found < 22 Then ws.Rows(found & ":" & found + (21 - found)).Insert
    
    End If
    
Next ws


Application.ScreenUpdating = True
MsgBox "Done!"
End Sub

My modified code for active sheet is as follows but I keep getting an error
'Run-time error '424':
Object required

I have highlighted the error that is shown in red below

Code:
Sub InsertRows_Activesheet()

Dim found As Long


With ActiveSheet


[COLOR=#ff0000]found = .Columns(2).Find(What:="*", After:=ws.Cells(1, 2), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row[/COLOR]
    If found < 22 Then ws.Rows(found & ":" & found + (21 - found)).Insert
    
    End With
MsgBox "Done!"
End Sub



If someone can advise where I am going wrong it would be very much appreciated

Thanks
Andy
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Probably need to get rid of the ws in this line

Code:
[COLOR=#FF0000]After:=ws.Cells(1, 2)[/COLOR]

Try...

Code:
[COLOR=#FF0000]After:=.Cells(1, 2)[/COLOR]
 
Upvote 0
Hi lrobbo314

Thanks so much, it worked great.

Feel stupid I overlooked that, but it is great that so many people on here give their time and advice.

Thanks again
Andy
 
Upvote 0
No problem. Don't feel dumb. Those moments happen to everyone. Coding has a lot of range in making you feel brilliant as well as giving you those, 'UGH!' moments.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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