excel - 即使使用 xlCelltypevisible,Row.Count 也会计算不可见的单元格

标签 excel vba count row

我正在尝试过滤有时可能导致没有数据的表。我正在尝试计算可见的行数以做出此决定。
如果只有一个标题行 - “无单元格”
如果有可见行 - “有提供者”
在下面的代码中,它似乎仍然计算过滤的行......

Sub Add_New_Name()

    Dim pTable1 As Range
    Dim pVisible As Range

    'Application.DisplayAlerts = False
    'Application.ScreenUpdating = False
            
    ' Select Roster & Clear Roster Table Filters
    Sheet8.Activate
    Sheet8.ListObjects("Table1").AutoFilter.ShowAllData
                    
    ' Set Variables
    Set pTable1 = Range("B2").CurrentRegion
    Set pVisible = pTable1.SpecialCells(xlCellTypeVisible)
                    
    ' Check for New Associate
    With Sheet8.ListObjects("Table1")
        .Range.AutoFilter Field:=23, Criteria1:="0"
        .Range.AutoFilter Field:=22, Criteria1:="Associate"
    End With
                    
    If pVisible.Rows.Count > 1 Then
        MsgBox "No Cells"
    Else
        MsgBox "Has Provider"
    End If
                    
End Sub

最佳答案

是否有任何过滤的行?

  • 您可以使用 On Error Resume Next如蒂姆威廉姆斯的回答所示。
  • 您不能在非连续范围上使用行数,因为它仅指该范围的第一个区域。因此,如果第一个数据行不可见,则无论有多少行可见,它都会返回 1。
  • 但是您可以在不连续的单列范围内使用单元格计数。

  • Option Explicit
    
    Sub Add_New_Name()
        
        Application.ScreenUpdating = False
        
        Dim cc As Long
        With Sheet8.ListObjects("Table1")
            If .ShowAutoFilter Then ' remove filter
                If .AutoFilter.FilterMode Then .AutoFilter.ShowAllData
            End If
            .Range.AutoFilter Field:=23, Criteria1:="0"
            .Range.AutoFilter Field:=22, Criteria1:="Associate"
            ' Get the cells count of any single column range!
            cc = .ListColumns(1).Range.SpecialCells(xlCellTypeVisible).Cells.Count
            .AutoFilter.ShowAllData ' remove filter
        End With
        
        Application.ScreenUpdating = True ' before the message box
        MsgBox IIf(cc = 1, "No Cells", "Has Provider")
        
    End Sub
    

    关于excel - 即使使用 xlCelltypevisible,Row.Count 也会计算不可见的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71549731/

    相关文章:

    vba - 使用 VBA 在单元格中包含引号的公式会引发错误

    excel - 在 Case Select 语句中使用数组作为参数

    excel - Vba检查单元格中是否部分粗体

    excel - VBA 嵌套错误 GoTo

    python - 在Python中计算集合中的大量数字的有效方法?

    r 计算每个变量每个 ID 的非缺失条目数

    vba - 如何在 VBA 中引用另一个(打开或关闭)工作簿并将值拉回? - Excel 2007

    vba - Excel VBA简单的if not语句

    excel - 如何使用VBA设置一定范围内的背景颜色?

    mysql - 我可以使用 MySQL Join 进行 COUNT 操作吗?