根据单元格值/格式过滤表

时间:2020-01-22 19:11:58

标签: excel vba

下面的代码需要稍作修改。 这是基于我发布的How to use If-Else statements to filter a table?

上一个问题

Column 14将被过滤,并具有会计编号格式的值。我想在Column 14中搜索不是$-的任何值,即$ 145,0450.56。如果存在一个$-单元格,则继续进行过滤。

我尝试使用<>来表示不等于$-的值,但这不起作用。

Sub DeleteJob()

Dim tbl As ListObject
Dim ws As Worksheet

  'Set reference to the sheet and Table.
  Set ws = Sheets("Line Item Summary")
  Set tbl = ws.ListObjects("Table1")
  ws.Activate

  'Clear any existing filters
  tbl.AutoFilter.ShowAllData

    With tbl.ListColumns(14)
        If Application.CountIf(.DataBodyRange, ">0") + _
           Application.CountIf(.DataBodyRange, "<0") = 0 Then
             Exit Sub
        End If
    End With

  'Apply Filter
  tbl.Range.AutoFilter Field:=14, Criteria1:=""

  'Delete Rows
  Application.DisplayAlerts = False

    tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete

  Application.DisplayAlerts = True

  '3Clear Filter
  tbl.AutoFilter.ShowAllData

End Sub

编辑

这是我编辑的行:
If Application.CountBlank(tbl.ListColumns(14).DataBodyRange) = 0 Then Exit Sub

当我运行代码时,它将忽略非值行(如下所示),并在一半时退出宏。代码需要识别出$-所在的行,然后继续进行过滤。

enter image description here

1 个答案:

答案 0 :(得分:1)

$-只是0,已格式化。

一种选择就是检查您的值是否不等于零:

With tbl.ListColumns(14)
    If Application.CountIf(.DataBodyRange,">0") + _
       Application.CountIf(.DataBodyRange,"<0") = 0 Then
         Exit Sub
    End If
End With

请注意,Application.CountIf(.DataBodyRange,"<>0")将包含空白单元格,这就是为什么您可以使用上面所示的“长途路线”的原因。

编辑:我想我倒了:

With tbl.ListColumns(14)
    If Application.CountIf(.DataBodyRange,"=0") = 0 Then
         Exit Sub
    End If
End With