简体   繁体   中英

VBA - If cell blank, delete entire row

shMassPrelim.Range("AG2:AG" & lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

The above code is what I'm using to look for blank cells in column AG. However, Column AG is based upon an If formula. Is this the reason why I'm getting back "No Cells Blank" error?

Try this?

Sub misc()
Dim cell As Range
Dim EMPTY_CELL As String
    EMPTY_CELL = ""
    For Each cell In Selection
        If Trim(cell.Value) = EMPTY_CELL Then
            cell.EntireRow.Delete
        End If
    Next
End Sub

This will filter and delete the blank rows

Sub DeleteStuff()
    Dim Rws As Long, Rng As Range

    Rws = Cells(Rows.Count, "AG").End(xlUp).Row
    Set Rng = Range(Cells(2, "AG"), Cells(Rws, "AG"))

    Application.ScreenUpdating = 0

    Range("AG:AG").AutoFilter Field:=1, Criteria1:="="
    Rng.SpecialCells(xlCellTypeVisible).EntireRow.Delete

    ActiveSheet.AutoFilterMode = 0

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM