
下面的公式效果很好如果我定義了每個單元格(一遍又一遍),但我需要一個循環,該循環將沿著 A 列和 B 列運行數百行。我已經嘗試過了,但似乎無法編寫一個循環,當它遇到底部的空白單元格時運行並停止。這需要能夠在具有不同選項卡名稱的多個電子表格上運行。
現在有效的範例: [我想要一個可以循環的公式] 我目前已將其寫出 100 行,因為我無法讓交叉起作用。 :-(
Sub Hidelines()
If Range("A1").Value = "No" Then
Rows("1:1").EntireRow.Hidden = True
ElseIf Range("B1").Value = "NEVER" Then
Rows("1:1").EntireRow.Hidden = True
End If
If Range("A2").Value = "No" Then
Rows("2:2").EntireRow.Hidden = True
ElseIf Range("B2").Value = "NEVER" Then
Rows("2:2").EntireRow.Hidden = True
End If
If Range("A3").Value = "No" Then
Range("E3").Select
Rows("3:3").EntireRow.Hidden = True
ElseIf Range("B3").Value = "NEVER" Then
Rows("3:3").EntireRow.Hidden = True
End If
End Sub
答案1
Sub HideRows()
Dim RowCount: RowCount = 1 ' Row you wish to start from
Dim ColIndex: ColIndex = 1 ' Column to look within (A = 1) - Never will be in ColIndex + 1
Do
If (LCase(Cells(RowCount, ColIndex).Value) = "no") Then
Cells(RowCount, ColIndex).EntireRow.Hidden = True
ElseIf (LCase(Cells(RowCount, ColIndex + 1).Value) = "never") Then
Cells(RowCount, ColIndex).EntireRow.Hidden = True
End If
RowCount = RowCount + 1
Loop Until IsEmpty(Cells(RowCount, ColIndex).Value)
End Sub
這將繼續向下移動每一行,直到遇到 列 中的空白儲存格ColIndex
。它將不區分大小寫地查看 的同一列No
或其右側的一列Never
,如果是,則隱藏該行。