VBA - 如果单元格为空,则删除整行

时间:2015-02-25 21:43:17

标签: vba excel-vba excel-2007 excel

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

以上代码是我用来在AG列中查找空白单元格的代码。但是,Column AG基于If公式。这是我回到“No Cells Blank”错误的原因吗?

2 个答案:

答案 0 :(得分:0)

试试这个?

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

答案 1 :(得分:0)

这将过滤和删除空白行

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