0

I'm looking to create a macro that deletes all rows that don't contain any data in Column B. Any help would be appreciated. This is all I got for now.

Sub DeleteAllEmptyBRows()
Dim lr As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
    For Each cell In Range("B1:B" & lr)
        If cell.Value = "" Then
            cell.Row.Delete
            Exit Sub
        End If
   Next cell
End Sub

2 Answers 2

3

You can use SpecialCells to do this in one quick line:

Range("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

I'd use the above, but also for your own knowledge, here's how you could do it following your code:

Sub DeleteAllEmptyBRows()
Dim lr As Long, i&

lr = Cells(Rows.Count, "B").End(xlUp).Row

For i = lr To 1 Step -1 'Since you're deleting rows, start at the end, and work upwards
    If Cells(i, 2).Value = "" Then
        Cells(i, 2).EntireRow.Delete
    End If
Next i

End Sub

Note that you have an Exit Sub in yours, after the first time a row is deleted. I removed that, since you want to loop through all cells in the range. Again, this is a loop so will take longer, and has more room for errors, than the simple one liner above.

Sign up to request clarification or add additional context in comments.

1 Comment

Realized I forgot to thank you. This is perfect, thank you so much!
1

You are missing some parameters:

Cells(cell.Row, 2).Delete Shift:=xlUp

If you need the entire row, just change to:

cell.Row.EntireRow.Delete

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.