Macro to delete Zero and blank Rows

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have a sheet called "Summary"

I have tried to write code to delete the rows from Row 25 onwards up to the last row in Col A, where Col B contains a zero or blank

The macro takes a few mins to run and there is a maximum of 1000 rows

It would be appreciated if someone could kindly amend my code

Code:
 Sub deleteRowsSummary()
    Dim lastRow As Long
    Dim i As Long
    With Sheets("Summary")
.Unprotect
    'Get the last row in column A
    lastRow = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Row
    
    'Loop through rows 25 to last row in column B
    For i = 25 To lastRow
        If Sheets("Summary").Range("B" & i).Value = "" Or Sheets("Summary").Range("B" & i).Value = 0 Then
            Sheets("Summary").Range("B" & i).EntireRow.Delete
            i = i - 1 'since the row is deleted, need to decrement i to re-check the same row number
        End If
    Next i
    .Protect
 End With 
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
When deleting you should loop from the bottom up, and to help speed things up you can turn off screen updating and set calculation to manual.
VBA Code:
Option Explicit

Sub deleteRowsSummary()
Dim lastRow As Long
Dim I As Long

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
   
    With Sheets("Summary")
        .Unprotect
        'Get the last row in column A
        lastRow = .Range("A" & Rows.Count).End(xlUp).Row

        'Loop through rows 25 to last row in column B
        For I = lastRow To 25 Step -1
            If .Range("B" & I).Value = "" Or Sheets("Summary").Range("B" & I).Value = 0 Then
                .Range("B" & I).EntireRow.Delete
            End If
        Next I
        .Protect
       
    End With
   
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
   
End Sub
Other than that, if you really want to speed things up look at filtering the data and deleting rather than looping.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,133
Messages
6,123,235
Members
449,092
Latest member
SCleaveland

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