How to Delete a Row Using Macro If a Cell Contains 0 in Excel (4 Methods)

The dataset below shows the Number of Visits, the Names of the Sites, and the Dates. Some sites have a cell value (Number of Visits) of 0. In such a situation, you might want to delete rows with the cell value of 0.

Dataset


Method 1 – Using Macro to Delete Certain Rows If Cell Contains 0

Step 1: Inserting a Module

  • Open a module by clicking Developer > Visual Basic.

How to Insert VBA Code

  • Go to Insert > Module.

How to Insert VBA Code

Step 2: Copying the VBA Code

  • Enter the following code:
Sub Delete_CertainRows()
Dim Sht As Worksheet
Dim last as Long
Dim i As Long
Set Sht = ActiveSheet
    last = Cells(Rows.count, "A").End(xlUp).Row
    For i = last To 14 Step 1
        If Cells(i, "D").Value = "0" Then
                Cells(i, "D").EntireRow.Delete
        End If
    Next i

End Sub

Deleting Certain Rows

Code Breakdown

  • I declared Sht as Worksheet to define the current worksheet. In addition, I also declared last and i as Long to store the row number.
  • Then, I turned on the current sheet by setting the ActiveSheet.
  • Immediately, I assigned the last to Cells(Rows.count, “A”).End(xlUp).Row to return the last row as the top cell of the bottom column.
  • Later, I utilized For Loop where i was assigned as the last To 14 Step 1. That means the loop would run till Row 14 and the increment was 1 for every row.
  • Lastly, I added an If statement to delete the row with a cell value of 0. The Delete function is attached to delete the entire row when the statement is true.

Step 3: Running the VBA Code

When you run the code (the keyboard shortcut is F5 or Fn + F5), you’ll get the following output.

Deleting Certain Rows

Read More: How to Use Macro to Delete Rows Based on Criteria in Excel


Method 2 – Deleting the Entire Row from the Defined Range When the Cell Value is Zero

  • Enter the following code into the newly created module:
Sub DeleteRow_DefinedRange()
 
Dim Rng As Range
 
For Each Rng In Range("D5:D14")
    If Rng.Value = "0" Then
        Rng.EntireRow.Delete
    End If
Next Rng
 
End Sub

Delete Entire Row from Defined Range When Cell Value is Zero

Code Breakdown

  •  I declared Rng as Range.
  • Then I assigned For Each statement to define each element of the pre-defined range i.e. D5:D14.
  • Along with that, I also added an If statement and Delete to delete the entire row when the statement is true.

However, the output will look as follows if you run the code.

Delete Entire Row from Defined Range When Cell Value is Zero

Read More: How to Apply VBA Code to Delete Rows Based on Multiple Cell Value


Method 3 – Using InputBox for Defining Range to Delete Entire Row

  • Enter the following code:
Sub DeleteRow_UsingInputBox()
    Set Rng = Application.Selection
    Title = "Delete Row Using InputBox"
    Set Rng = Application.InputBox("Please Select the Cell Range:", Title, Rng.Address, Type:=8)
    Do
        Set cell = Rng.Find("0", LookIn:=xlValues)
        If Not cell Is Nothing Then
           cell.EntireRow.Delete
        End If
    Loop While Not cell Is Nothing
End Sub

excel macro delete row if cell contains 0 Using InputBox for Defining Range

Code Breakdown

  • Here, I set Rng utilizing InputBox
  • Then I assigned Do Loop to repeat and transfer the control of the If Not statement.
  • Meanwhile, I specified the cell using the Find to find 0.
  • In addition, I added an If Not statement with the Delete to delete row when the statement is true.

 

  • You’ll see the following dialog box to enter the range from where you want to delete rows with specific criteria.
  • Enter $D$5:$D$14 manually or drag down the cursor from D5 to D14 cell.

excel macro delete row if cell contains 0 Using InputBox for Defining Range

You’ll get the following output.

excel macro delete row if cell contains 0 Using InputBox for Defining Range

Read More: How to Use VBA to Delete Empty Rows in Excel


Method 4 – Deleting a Row If Cell Contains 0 across Multiple Sheets

For example, the dataset has 3 separate working sheets. Sheet 1 includes the same information as the dataset, but Sheet 2 has a slightly different Number of Visits.

excel macro delete row if cell contains 0 across Multiple Sheets

The following picture denotes the different information about Sheet3.

excel macro delete row if cell contains 0 across Multiple Sheets

  • Enter the following code:
Sub DeleteRows_MultipleSheets()
Dim Sht As Worksheet
Dim last As Long
Dim i As Long
For Each Sht In Worksheets(Array("Sheet1", "Sheet2", "Sheet3"))
    Sht.Activate
    last = Cells(Rows.count, "A").End(xlUp).Row
    For i = last To 14 Step 1
        If Cells(i, "D").Value = "0" Then
                Cells(i, "D").EntireRow.Delete
        End If
    Next i
 Next Sht
End Sub

excel macro delete row if cell contains 0 across Multiple Sheets

Code Breakdown

  • I declared Sht as Worksheet. In addition, I also declared last and i as Long to store the row number.
  • Then, I specified the 3 sheets with their name using the Worksheets, And Each statement is set to define each element.
  • The rest are similar to the first method.

After running the code, you’ll get the output in the case of Sheet1.

excel macro delete row if cell contains 0 across Multiple Sheets

You’ll get the following output in the case of Sheet2.

excel macro delete row if cell contains 0 across Multiple Sheets

The output for Sheet3 will be as follows.

excel macro delete row if cell contains 0 across Multiple Sheets

Read More: Excel VBA to Delete Row If Cell Contains Partial Text


Download the Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Md. Abdul Kader
Md. Abdul Kader

MD. ABDUL KADER is an engineer with a talent for Excel and a passion for VBA programming. To him, programming is like a time-saving wizard, making data manipulation, file handling, and internet interactions a breeze. His skill set extends to SWM, GIS, RS, and SPSS. He holds a B.Sc in Urban & Regional Planning from Chittagong University of Engineering and Technology and has shifted to become a content developer. In this role, he crafts technical content centred around... Read Full Bio

2 Comments
  1. Hello Abdul,

    I am struggling with my data set.

    Using your macros (method 2 suits best) seems to miss deleting rows containing zero if they are adjacent (above / below) another row containing zero. Your example does not replicate this.

    • Hello T C Millichap,

      It sounds like the macro might not be checking all rows properly when there are consecutive zeros. This could happen if the loop skips over rows after deleting one. You can modify the code to ensure it doesn’t skip rows by adjusting how the loop is set up.

      Sub DeleteRow_DefinedRange()
          Dim Rng As Range
          Dim i As Long
      
          For i = 14 To 5 Step -1 ' Loop backwards to prevent skipping rows
              If Cells(i, 4).Value = 0 Then
                  Rows(i).Delete
              End If
          Next i
      End Sub

      By looping backwards, you prevent skipping rows after deletion. This should address the issue of consecutive zeros. Let me know if you need further adjustments!

      Regards
      ExcelDemy

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo