Delete rows using macro, loop to all sheets

kev8279

New Member
Joined
Oct 12, 2009
Messages
29
Hi guys,

I need a Macro that will look for 0 in column A and delete the row. I then need that to loop to do the same on all work sheets. I've been trawling the net for ages and can't come up with anything. Can anyone help please?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:

Code:
Sub deleteRow()

For Each sh In ActiveWorkbook.Sheets

    ActiveWorkbook.Sheets(sh.Name).Activate
    
    For i = [a65000].End(xlUp).Row To 1 Step -1
        If Cells(i, 1) = "0" Then Rows(i).Delete shift:=xlUp
    Next i

Next sh

End Sub

Good luck
 
Upvote 0
I prefer to filter and delete, rather than looping. It's generally quicker, as long as you disable calculation first.
Code:
Sub Macro1()
    Dim Rng As Range
    Dim Sht As Worksheet
    
    Application.Calculation = xlCalculationManual
    
    For Each Sht In ActiveWorkbook.Sheets
        Sht.Activate
        Set Rng = Range("A1").CurrentRegion
        Rng.AutoFilter
        Rng.AutoFilter Field:=1, Criteria1:="0"
        Rng.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        Rng.AutoFilter
        Set Rng = Nothing
    Next Sht
    
    Application.Calculation = xlCalculationAutomatic
    
End Sub

Denis
 
Upvote 0
That's great, thanks guys.

I've used the first one as some of the cells are hidden and grouped, however in order for this to work I need to unhide all of the cells. Is there a way to get around this?

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

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