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
行高为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”当然。
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
你需要第二个循环来遍历Range.SpecialCells( xlCellTypeVisible )的Range.Areas 。 每个区域可以是一行或多行。
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
看来你想要倒退,所以我继续这个方向。 如果打算删除行,有更简单的方法来做到这一点。
Dim hiddenColumn: hiddenColumn = "B" For i = 1 To 10 If Range(hiddenColumn & i).EntireRow.Hidden = False Then 'logic goes here.... End If Next