Run Macro on Each Sheet of Active Workbook

zach9208

Board Regular
Joined
Dec 15, 2015
Messages
117
The following code does not run across all of my worksheets. It seems to just run the same code on the first tab multiple times. Can someone help me fix this? My best guess is that that the ActiveCell member is causing this issue, but I do not know how to fix this.


Code:
Sub DeleteRowsfromEachSheet()

Dim wk As Worksheet
For Each wk In ThisWorkbook.Worksheets
Dim Counter As Long


Range("B1").Select ' Start at B1
For Counter = 1 To 100
If ActiveCell.Value Like "*Allocated Operating Exp OE Comp and Other Excl Stock Comp*" Then
ActiveCell.EntireRow.Delete ' Delete Row
Else
ActiveCell.Offset(1, 0).Select  ' Move down a row
End If
Next Counter
Next wk


End Sub
 

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).
Looping through the list of worksheets does not actually select or activate them.
So you either need to select each sheet before you start working on them, like this:
Code:
For Each wk In ThisWorkbook.Worksheets
    wk.Activate
or include the worksheet reference in each range reference, i.e.
Code:
For Each wk In ThisWorkbook.Worksheets
    wk.Range("B1")
Also, you want to avoid using ActiveCell in your code if possible.
And when deleting rows in a range, it is always best to loop through your range backwards.

This code should do what you want:
Code:
Sub DeleteRowsfromEachSheet()

    Dim wk As Worksheet
    Dim Counter As Long
    
    For Each wk In ThisWorkbook.Worksheets
        For Counter = 100 To 1 Step -1
            If wk.Range("B" & Counter).Value Like "*Allocated Operating Exp OE Comp and Other Excl Stock Comp*" Then
                wk.Range("B" & Counter).EntireRow.Delete ' Delete Row
            End If
        Next Counter
    Next wk

End Sub
 
Last edited:
Upvote 0
Thanks, Joe. Your code is exactly what I was looking for! Thanks for the explanation as well.

Looping through the list of worksheets does not actually select or activate them.
So you either need to select each sheet before you start working on them, like this:
Code:
For Each wk In ThisWorkbook.Worksheets
    wk.Activate
or include the worksheet reference in each range reference, i.e.
Code:
For Each wk In ThisWorkbook.Worksheets
    wk.Range("B1")
Also, you want to avoid using ActiveCell in your code if possible.
And when deleting rows in a range, it is always best to loop through your range backwards.

This code should do what you want:
Code:
Sub DeleteRowsfromEachSheet()

    Dim wk As Worksheet
    Dim Counter As Long
    
    For Each wk In ThisWorkbook.Worksheets
        For Counter = 100 To 1 Step -1
            If wk.Range("B" & Counter).Value Like "*Allocated Operating Exp OE Comp and Other Excl Stock Comp*" Then
                wk.Range("B" & Counter).EntireRow.Delete ' Delete Row
            End If
        Next Counter
    Next wk

End Sub
 
Upvote 0
You are welcome. Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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