如何在应用筛选器后选择第二个可见行



通常在尝试选择第一行过滤后的数据时,我会使用以下代码,它运行得非常好:

ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 3).Select

我认为尝试选择第二行过滤数据的简单解决方案是将"单元格(1,3("中的1改为2,如下所示:

ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(2, 3).Select

然而,这只是将我带到一个对过滤器隐藏的单元格。

我做错了什么?

谢谢!

您可以尝试以下操作:

Sub SelectVisibleRowByIndex()
Dim lastRow As Long, visibleCount As Long, i As Long, rowIndex As Long

'Set the index of the row you want to select, you can select the 2nd, 3rd, etc.
rowIndex = 2

' Get the last row in the sheet
lastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row

' Loop through each row and count the visible rows
For i = 1 To lastRow
If Not Rows(i).Hidden Then
visibleCount = visibleCount + 1
If visibleCount = rowIndex Then
' Once we find the row with the specified index, select the entire row
Rows(i).Select
Exit For
End If
End If
Next i
End Sub

最新更新