我有一个可过滤的联系人数据库和一个用户表单。我想要的是userform上的2个计数器。 lblSelection应该给出数据集的总量。而lblCount应该在整个数据集中给出所选单元格的位置。
像“你在第45集中的300”这样的东西
现在,当数据库被过滤时,它应该读取例如“你在第6组中的67”,仅使用可见单元格并计算该范围内所选单元格的位置。
这是我到目前为止使用的简单代码:
Public Sub CountReset()
'Display Count
If ActiveSheet.FilterMode = False Then
Me.lblCount = activecell.Row - 1
Me.lblSelection = activecell.Offset(-1, 0).End(xlDown).Row - 1
Else
Me.lblCount = ????
Me.lblSelection = Base.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
End If
End Sub
非常感谢您的帮助!
答案 0 :(得分:1)
我认为您需要这样的循环:
Public Sub CountReset()
Dim n As Long
Dim rCell As Range
'Display Count
If ActiveSheet.FilterMode = False Then
Me.lblCount = ActiveCell.Row - 1
Me.lblSelection = ActiveCell.Offset(-1, 0).End(xlDown).Row - 1
Else
n = -1
With Base.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible)
For Each rCell In .Cells
n = n + 1
If rCell.Row = ActiveCell.Row Then Exit For
Next
Me.lblCount = n
Me.lblSelection = .Cells.Count - 1
End With
End If
End Sub
答案 1 :(得分:0)
你几乎得到了它。
Public Sub CountReset()
Me.lblCount = activecell.Row - 1 ' this will be the same if it's filtered or not
If ActiveSheet.FilterMode = False Then
Me.lblSelection = activecell.Offset(-1, 0).End(xlDown).Row - 1
Else
'assuming base is a defined table or named range ... also no need to filter it because it's already filtered
Me.lblSelection = Base.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
End If
End Sub