【问题标题】:Problem with filters and entire row delete过滤器和整行删除问题
【发布时间】:2019-05-24 11:13:44
【问题描述】:

我对@9​​87654321@ 有疑问。
有时我的宏会从工作表中删除所有行,有时它会回到上一个错误处理和跳过顺序。

我尝试过滤值,将它们分配给范围并删除范围,但程序执行大约需要 20 分钟,所以我不得不终止处理。

wb_current.Worksheets("RH").Range("A6:V6").AutoFilter Field:=22, Criteria1:=Array("#N/A", ""), Operator:=xlFilterValues
With wb_current.Worksheets("RH")
    lng_last_row_main = .Cells(Rows.Count, 1).End(xlUp).Row
    If lng_last_row_main > 6 Then
        .Rows("7:" & lng_last_row_main).EntireRow.Delete
    End If
    .AutoFilterMode = False
    lng_last_row_main = .Cells(Rows.Count, 1).End(xlUp).Row
End With

我想设置一个过滤器来显示特定列中带有空白或 NA 的行,然后删除那些包含这些值的行,然后设置过滤器并获取没有空白行的表格。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    Aleksander,也许我可以帮助您解决问题。

    根据我对您的问题和预期效果的理解,真的有必要实际使用过滤器吗?如果您使用它只是为了能够删除某列中的所有空白行,那么带有If 函数的简单循环就足够了。

    请在下面查看我提出的代码:

    Sub delete_blank_rows()
    Dim lastRow As Long
    Dim cel As Range
    
    lastRow = ThisWorkbook.Worksheets("RH").Cells(Rows.Count, 1).End(xlUp).Row
    
    'loop through every cell in column C until the last row (lastRow is taken from column A)
    For Each cel In ThisWorkbook.Worksheets("RH").Range("C1:C" & lastRow)
    
        'function that evaluates if cell in column C is blank/#N/A and deletes it if yes
        If cel.Value = "" Or cel.Value = "#N/A" Then
            cel.EntireRow.Delete
        End If
    Next cel
    
    End Sub
    

    这在我的示例数据上完美运行。请记住,标题位于我的示例数据的第一行 - .Range("C1:C" & lastRow)

    我希望这能解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-21
      • 2021-01-10
      • 1970-01-01
      • 2010-11-07
      • 2021-12-14
      • 2013-09-27
      • 2017-08-07
      • 2016-05-09
      相关资源
      最近更新 更多