Selection.SpecialCells() 方法返回意外范围(Excel VBA)

Max*_*gal 3 excel vba

当我选择包含三个单元格的范围(例如 B3:B5)时,该方法将按预期运行并显示包含“3”、“4”和“5”的三条消息。

Sub visTest()
    Dim c As Range
    For Each c In Selection.SpecialCells(xlCellTypeVisible)
        MsgBox c.row
    Next c
End Sub
Run Code Online (Sandbox Code Playgroud)

问题是当我仅选择一个单元格时:Selection.SpecialCells(xlCellTypeVisible)返回工作表上的所有可见单元格并从单元格 A1 开始。

如何使其仅返回一个选定单元格内的一个可见单元格?为什么会出现这个问题?

谢谢!

Gar*_*ent 7

这将执行正确的限制:

Sub visTest()
    Dim c As Range
    For Each c In Intersect(Selection, Selection.SpecialCells(xlCellTypeVisible))
        MsgBox c.Row
    Next c
End Sub
Run Code Online (Sandbox Code Playgroud)