1

在 Excel Vba 中,我正在对引用 ListObject 的表列进行过滤;

With TableX
.Range.AutoFilter Field:=15, Criteria1:="100"
End With

我想在执行进一步的代码之前测试过滤后的列,因为过滤器可能不会返回与 100 的值匹配的行。我找到了一些关于 TotalsCalculation 方法的信息,但很难对其进行编码。关于我在这个简单示例中缺少的任何想法?

4

2 回答 2

0

参考可见 Excel 表行

Dim vrg As Range ' Visible Range

With Sheet1.ListObjects("Table1") ' tbl
    
    If .AutoFilter.FilterMode Then .AutoFilter.ShowAllData
    
    .Range.AutoFilter 15, "100"
    
    On Error Resume Next
        Set vrg = .DataBodyRange.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    
    .AutoFilter.ShowAllData
    
End With

If Not vrg Is Nothing Then ' visible rows found
    ' Continue, e.g.:
    Debug.Print vrg.Address(0, 0)
    'vrg.Delete Shift:=xlShiftUp ' delete visible rows

Else
    Debug.Print "No visible rows found."
End If
于 2022-01-05T10:51:33.923 回答
0

下面的代码将测试是否有零行。为了能够正确使用该SpecialCells(xlCellTypeVisible)方法,我包含了始终存在的标题行,因此如果可见单元格行数 > 1,您可以确保至少有一个值为 100。

With TableX

    .Range.AutoFilter Field:=15, Criteria1:="100"

    If Intersect(.Range, .ListColumns(15).Range).SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then
        'do stuff
    End If

End With
于 2018-01-17T23:16:19.163 回答