0

This code works if i have two columns side by side both with zeros in them. But i need it to delete the row if the cell on the right has a value of 0. The cell on the left is a word if that makes a difference? Think of a shopping list with the items and their amounts needed in the column on the right

Sub DeleteRow()
    Dim r As Long
    Dim LastRow As Long

    LastRow = Cells(Rows.Count, "b").End(xlUp).Row
    For r = LastRow To 1 Step -1

        If Cells(r, 1) = 0 Then
            Rows(r).Delete
        End If

    Next r
End Sub
BigBen
  • 46,229
  • 7
  • 24
  • 40
rayad
  • 125
  • 1
  • 6
  • `If Cells(r, 2) = 0 Then` if you want to look at column B. Are you asking to add the logic to look at `cells(r, 1)` to see if it's text too? – BigBen Jan 30 '20 at 18:07
  • 1
    [Alternative](https://stackoverflow.com/questions/11317172/delete-row-based-on-condition) to looping :) – Siddharth Rout Jan 30 '20 at 18:12

1 Answers1

1

The count of columns starts at 1 not 0 so the if statement should be:

If Cells(r, 2) = 0 Then

This is if your "right" column is column B, which it appears so from your initialization of LastRow.

  • Thank you so much! I'm new to all this VBA stuff and spent way too much time trying to figure this out for myself haha. Thanks again! – rayad Jan 30 '20 at 19:43