假设我有10行数据。我对数据应用了一些过滤器,行号7和9 过滤 OR 可见。
我想遍历数据(10行)并在空白列(假设C列),中输出字符串“Is Visible” 用于可见行(即范围C7和范围C9)。
答案 0 :(得分:21)
选择要隐藏的前10行中的一些,然后尝试运行此
Option Explicit
Sub CheckIfVisible()
Dim i As Integer, x As Integer
x = 0
For i = 1 To 10
With Excel.ThisWorkbook.ActiveSheet
If .Rows(i).EntireRow.Hidden Then
Else
.Cells(15 + x, 1) = "Row " & i & "is visible"
x = x + 1
End If
End With
Next i
End Sub
这是你正在寻找的那种循环吗? 也许您可以向我们展示您的循环,以便我们可以看到您的问题所在?
答案 1 :(得分:0)
自从我来到这里寻找前段时间以来,这可能对将来的Google员工有用。
如果您要按单元格/行进行操作,则可以直接访问 .EntireRow.Hidden bool单元格。
在下面的示例中,这只是一个遍历Selection中每个单元格的ForEach循环,并且仅读取该单元格中的属性,并基于.Hidden为True / False进行计数/着色。
如果您正在测试过滤范围,则需要逐行选择范围之外的内容,因为如果隐藏行落在该范围的倒数第二/最后一行中,则可能无法捕获它们,因此请选择选择后的第一个可见行可以避免这种情况。
对于大范围(超过10,000行)而言,效率极低
Sub HowManyHiddenCells()
Dim MyCell, MyRange As Range
Dim CellCountAll, CellCountVisible, CellCountHidden As Integer
Set MyRange = Selection
For Each MyCell In MyRange
':: IGNORE EMPTY ::
If Len(MyCell.text) > 0 Then
If MyCell.EntireRow.Hidden Then
MyCell.Interior.Color = RGB(255, 220, 200)
':: Count of hidden cells in range
CellCountHidden = CellCountHidden + 1
':: Do Column C Text! ::
MyCell.Offset(0, 2).FormulaR1C1 = "I was hidden! "
End If
If MyCell.EntireRow.Hidden = False Then
MyCell.Interior.Color = RGB(200, 255, 180)
':: Count of visible cells in range
CellCountVisible = CellCountVisible + 1
End If
':: Count of all cells in range
CellCountAll = CellCountAll + 1
End If
Next MyCell
MsgBox "Cells total " & CellCountAll & vbNewLine & "Hidden : " & CellCountHidden & vbNewLine & "Visible : " & CellCountVisible, vbOKOnly + vbInformation, "Count of hidden vs visible"
End Sub
Exmaple of script in action - on a filtered range, highlighting the hidden in red
答案 2 :(得分:-1)
受@whytheq的启发,我想到了这一点,因此它遍历了所有可见行选择中的:
Sub Loop_through_selected_rows()
Dim rng As Range: Set rng = ActiveWindow.RangeSelection
Dim i As Integer
For i = 0 To rng.Rows.Count - 1
If Cells(rng.Row + i, 1).EntireRow.Hidden Then
Else
Cells(rng.Row + i, 1).Range("A1:E1").Select 'Set Range within row to your needs
' Do something here
End If
Next
End Sub