0

我有下面的代码循环遍历选定范围的每一行。但是,当仅选择一个单元格时,代码会循环遍历工作表中的每一行,而不仅仅是处理一个实例。

我需要做什么才能让 for 循环在选择单个单元格时只处理一行?

Dim myRange as Range
Dim currRow as Range

Set myRange = Selection

For Each currRow In myRange.Rows.SpecialCells(xlCellTypeVisible)
    MsgBox currRow.Address
Next currRow

谢谢,

4

2 回答 2

1

我不知道代码行为如此的原因。看起来不错。
但要得到你想要的,试试这个:

Dim myRange As Range
Dim currRow As Range

Set myRange = Selection

If myRange.Rows.count = 1 And myRange.Columns.count = 1 Then
    For Each currRow In myRange.Rows
        MsgBox currRow.Address
    Next currRow
Else
    For Each currRow In myRange.Rows.SpecialCells(xlCellTypeVisible)
        MsgBox currRow.Address
    Next currRow
End If

希望这有效。

于 2013-10-02T07:47:55.857 回答
0

只需在代码中添加一个 if 语句来处理隐藏行:

    Dim myRange As Range
    Dim currRow As Range

    Set myRange = Selection

    For Each currRow In myRange
        If currRow.EntireRow.Hidden = False Then
            'Place your code here.
            Debug.Print currRow.Address
        End If
    Next currRow
于 2013-10-02T13:10:11.873 回答