Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在选定范围的行中循环时处理单个单元格_Vba_Excel_For Loop_Row - Fatal编程技术网

Vba 在选定范围的行中循环时处理单个单元格

Vba 在选定范围的行中循环时处理单个单元格,vba,excel,for-loop,row,Vba,Excel,For Loop,Row,我有下面的代码来循环选择范围的每一行。但是,当只选择一个单元格时,代码将在工作表中的每一行循环,而不仅仅处理一个实例 我需要做什么才能使for循环在选择单个单元格时只处理一行 Dim myRange as Range Dim currRow as Range Set myRange = Selection For Each currRow In myRange.Rows.SpecialCells(xlCellTypeVisible) MsgBox currRow.Address Ne

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

我需要做什么才能使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

谢谢,

我不知道为什么代码会这样。看起来不错。
但要得到您想要的,请尝试以下方法:

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

希望这能起作用。

只需在代码中添加一个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