VBA仅通过可见细胞循环

时间:2016-06-09 18:53:07

标签: excel vba excel-vba

我使用下面的代码循环遍历每一行但是我只想循环遍历B列中的可见单元格(因为我已经过滤掉了我想要忽略的值),范围是第10行-194。有谁知道我会怎么做?

For X = 192 to 10 Step -1
    If Range("B" & X).Text = "" Then **This needs to change to visible cells only but not sure how!
        Code required insert here
    Else
    End If
Next X

4 个答案:

答案 0 :(得分:4)

Dim cell as Range
With Range("B10:B192").SpecialCells(xlCellTypeVisible)
   For X = .Rows.Count to 1 Step -1
       Set cell = Range("A" & X) ' this sets the current cell in the loop
   Next X
End With

答案 1 :(得分:2)

行高为0表示该行被隐藏。所以你可以检查那个

For X = 192 to 10 Step -1
    If Worksheets("Sheet1").Rows(X).RowHeight > 0 Then
        Code required insert here
    End If
Next X

假设您正在处理“Sheet1”。

答案 2 :(得分:2)

您需要第二个循环来遍历Range.Areas Range.SpecialCells(xlCellTypeVisible。每个区域可以是一行或多行。

    Dim a As Long, r As Long
    With Range("B10:B192").SpecialCells(xlCellTypeVisible)
        For a = .Areas.Count To 1 Step -1
            With .Areas(a)
                For r = .Rows.Count To 1 Step -1
                    'Debug.Print .Cells(r, 1).Address(0, 0)
                    'Debug.Print .Cells(r, 1).Text
                    If .Cells(r, "B").Text = "" Then
                        'Code required insert here
                    End If
                Next r
            End With
        Next a
    End With

似乎你想要向后循环,所以我继续这个方向。如果打算删除行,则有更简单的方法可以做到这一点。

答案 3 :(得分:1)

Dim hiddenColumn: hiddenColumn = "B"
For i = 1 To 10
    If Range(hiddenColumn & i).EntireRow.Hidden = False Then
        'logic goes here....
    End If
Next