使用Excel删除表中的空白行

时间:2017-05-02 02:38:13

标签: excel vba excel-vba

我想创建一个宏来遍历表并根据B:E列中的条件删除空行,而不删除表的倒数第二行。表B的第二行到最后一行在列B:E中总是为空,所以我希望它跳过该行。这是我在下面的内容,但我不知道如何将其修改为正确的格式。

以下是B,C,D,E

列的名称

客户端
管道阶段(选择下拉列表)
预测关闭(选择下拉列表)
潜在机会

P.S我是非常新的宏,所以请耐心等待我。

Sub Delete_Table_Rows()

Dim tblrows As Range
Dim client As Long
Dim tbl As ListObjects
Dim ws As Worksheet

With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False

Set ws = Sheets(“sheet1”)
set tblrows = ws.worksheets("sheet1").Range(“table1, [client]").Value = ""

    For client = Selection.Rows.Count To 1 Step -1
        If WorksheetFunction.CountA(Selection.Rows(client)) = “” Then
            Selection.Rows(client).EntireRow.Delete
        End If
Next i
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
End With
End Sub

1 个答案:

答案 0 :(得分:1)

也许是这样的......

Sub DeleteBlankRowsFromTable()
Dim ws As Worksheet
Dim tbl As ListObject
Dim i As Long, FirstRow As Long, LastRow As Long
Dim rng As Range
With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
End With

Set ws = Sheets("Sheet1")
Set tbl = ws.ListObjects("Table1")

FirstRow = Range("Table1[Client]").Row

LastRow = tbl.Range.Cells(tbl.Range.Cells.Rows.Count, 1).Row

For i = LastRow - 2 To FirstRow Step -1
    Set rng = ws.Range(ws.Cells(i, 2), ws.Cells(i, 2).End(xlToRight))
    If WorksheetFunction.CountBlank(rng) = 4 Then
        ws.Rows(i).Delete
    End If
Next i

With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
End With
End Sub