我正在尝试过滤有时可能导致没有数据的表。我正在尝试计算可见的行数以做出此决定。
如果只有一个标题行 - “无单元格”
如果有可见行 - “有提供者”
在下面的代码中,它似乎仍然计算过滤的行......
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
如蒂姆威廉姆斯的回答所示。 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/