作者:爱你无丶止境空间 | 来源:互联网 | 2023-05-19 14:33
在excel表中应用过滤器后,任何人都可以帮我找到第n个元素的绝对行数.
例如,我已经过滤并且具有可见范围的数据元素.现在,当没有过滤器打开时,此过滤范围中的第20行(第n行)可能是第60行(绝对意义).有没有办法使用VBA查找绝对行号?
1> Lopsided..:
最简单的方法是特殊细胞.见下文:
Sub test()
Dim i As Integer, j As Integer, k As Integer
Dim Report As Worksheet
Set Report = Excel.ActiveSheet ' Store the current worksheet in a variable (always a good idea)
Dim visRng As Range ' Creating a range variable to store our table, excluding any rows that are filtered out.
Set visRng = Report.UsedRange.SpecialCells(xlCellTypeVisible) ' Select only rows within the used range that are visible.
Dim r As Range
For Each r In visRng.Rows ' Loop through each row in our visible range ...
MsgBox (r.Row) ' ... and retrieve the "absolute" row number.
Next
End Sub
编辑
汤姆声称这种方法不起作用,但我很确定它能满足您的要求.例:
这是我原来的测试表 - 未经过滤,因此您可以看到我们正在做的事情.
![未经过滤](https://img.php1.cn/3cd4a/1eebe/cd5/dc7ef30f57b727c7.jpeg)
然后我们在表格的中间某处过滤几个值...
![过滤](https://img.php1.cn/3cd4a/1eebe/cd5/60405fda58cd0acd.webp)
现在,当我们运行上面发布的脚本时,我们的消息框将显示每个未过滤行的"绝对"行号.结果是1,3,4,5和7.