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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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