我想获得过滤表中单元格的值。它似乎只对第一个值有效,但是从那里它也计算隐藏值。
我正在尝试获取有关表中特定值的更多信息。对于某些请求,这是一种逐步筛选过程。组合框用于选择特定的感兴趣区域,然后在列表框中显示该区域内所有值的“标题”。然后,应该可以看到在列表框中选择的请求的详细描述。
当在列表框中选择一个请求时,它使用索引号在表中找到右行并从那里获取描述。
Private Sub ListBox1_Click()
Dim tbl As ListObject
Dim IndexNmr As Integer
ComboValue = ComboBox1.Value
'Store Table Object to a variable and clear all filters
Set tbl = Worksheets("Data").ListObjects("Tabel1")
tbl.AutoFilter.ShowAllData
'Filter Table Object based on ComboValue
tbl.Range.AutoFilter Field:=8, Criteria1:= _
ComboValue
'Find selected index in List Box
For x = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(x) = True Then
IndexNmr = x + 1
End If
Next x
'Find Caption in Table based on the index number
CaptionString = tbl.DataBodyRange.Columns(10).SpecialCells(xlCellTypeVisible).Cells(IndexNmr).Value
Label1.Caption = CaptionString
End Sub
它似乎只对第一个可见值有效,我可以通过以下方式产生它:
`CaptionString = tbl.DataBodyRange.Columns(10).SpecialCells(xlCellTypeVisible).Cells(1).Value`
但是当IndexNmr是ex。 2,即使它已隐藏,它也只将第一个可见行下面的下一行记录下来。
实际上,正如您所看到的,SpecialCells
不能那样工作(可怜,似乎应该如此)
您必须找到其他方法来访问第n个可见行。这是一种方法的演示
Sub Demo()
Dim lo As ListObject
Dim lr As ListRow
Set lo = ActiveSheet.ListObjects(1)
Set lr = FindNthVisibleRow(lo, 2)
If Not lr Is Nothing Then Debug.Print lr.Range.Address
End Sub
Function FindNthVisibleRow(lo As ListObject, Idx As Long) As ListRow
Dim RwCnt As Long
Dim lr As ListRow
If Idx <= 0 Then Exit Function
For Each lr In lo.ListRows
If lr.Range.EntireRow.Hidden = False Then
RwCnt = RwCnt + 1
If Idx = RwCnt Then
Set FindNthVisibleRow = lr
Exit For
End If
End If
Next
End Function