gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:08:58 26 4
gpt4 key购买 nike

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

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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com