0

I'm new to macros and scripting in general.

I'd like to delete the row if columns 3, 4 and 5 are empty.

Sub DeleteRows()
Columns(3).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Works for when the case is just row 3, but I've tried various methods to get it to work as intended; they all result in an error of some sort.

So, how can I do it?

1 Answer 1

1

Try this:

Sub DeleteRows()
For i = 3 to 5
    Columns(i).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Next

The only problem with this is that if there is no blank cells it will return you with an Error. The alternative to this is to check if you have blank cells before the EntireRow.Delete.

Here's how you can do it:

Sub DeleteRows()
Dim rngBlanks as Range
Dim i as Integer
For i = 3 to 5
    On Error Resume Next
    Set rngBlanks = Columns(i).SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If Not rngBlanks is nothing then
        rngBlanks.EntireRow.Delete
    End if
Next

let me know if this works and if you need more explanation!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.