How to resolve error 400 in excel-vba

Abhinn Wasav picture Abhinn Wasav · Jun 17, 2017 · Viewed 23.4k times · Source

I am writing an excel-vba to find and delete entire row if having blanks cell in a particular column. My macro is working fine if there is at least one blank cell but showing error 400 if there is no blank cell. My code is

Sub GPF_Sign()
Dim i As Integer, n as integer
Dim LROW As Long

    LROW = Sheets("GPF").Range("B200").End(xlUp).Row

    n = Range("D9:D" & LROW).SpecialCells(xlCellTypeBlanks).Cells.Count
    If n > 0 Then
        Range("D9:D" & LROW).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End If
End Sub

Answer

A.S.H picture A.S.H · Jun 17, 2017

You can use On Error Resume Next, but this is not a usually recommended approach because it may mask other errors. Instead, try computing n in an error free way:

n = Application.CountIf(Sheets("GPF").Range("D9:D" & LROW), "")

yet another, still better way is to use AutoFilter:

Sub GPF_Sign()
  With Sheets("GPF").Range("D8:D200")
    .AutoFilter 1, ""
    .Offset(1).EntireRow.Delete
    .AutoFilter
  End With
End Sub