如何在Excel中隐藏一系列行,直到值更改

所以首先我在我的Excel工作簿中有一个基于单元格值隐藏行的macros。 这工作。 首先这是我的工作表的样子:

我的工作表的屏幕截图

这是我的代码:

Option Explicit Sub Hide() Application.ScreenUpdating = False Dim wks As Worksheet Dim Lastrow As String Dim Rng As Range Dim cell As Range On Error Resume Next For Each wks In ThisWorkbook.Worksheets With wks wks.Select Rows.Hidden = False Lastrow = Range("H" & Rows.Count).End(xlUp).Row ' Set Rng = Range("H1:H" & Lastrow) ' For Each cell In Rng If cell.Value = "Closed" Then ' cell.EntireRow.Hidden = True End If Next cell End With Exit For Next wks MsgBox "All Rows containing Closed in Column H have been hidden", vbInformation, "Information" Application.ScreenUpdating = True End Sub 

因此,这将隐藏我的第一个工作表中的“H”列中包含“closures”的任何行。

但是,正如你可以在我的打印屏幕上看到的,我想隐藏第一个metclosures值和第二个closures值之间的所有行。 直到最后一行。 但是我不知道该怎么做,请问有人能帮我吗?

要做到这一点,试试这个:

 Option Explicit Sub Hide() Application.ScreenUpdating = False Dim wks As Worksheet Dim Lastrow As String Dim Rng As Range, i As Long Dim cell As Variant For Each wks In ThisWorkbook.Worksheets wks.Rows.Hidden = False Lastrow = wks.Range("H" & Rows.Count).End(xlUp).Row If Lastrow > 1 Then cell = wks.Range("H1:H" & Lastrow).Value i = 1: Set Rng = Nothing While i <= Lastrow For i = i To Lastrow If LCase(cell(i, 1)) = "closed" Then Exit For Next For i = i To Lastrow If LCase(cell(i, 1)) = "closed" Or cell(i, 1) = "" Then If Rng Is Nothing Then Set Rng = wks.Rows(i) Else Set Rng = Union(Rng, wks.Rows(i)) End If Else Exit For End If Next Wend If Not Rng Is Nothing Then Rng.EntireRow.Hidden = True End If Next MsgBox "All Rows containing Closed in Column H have been hidden", vbInformation, "Information" Application.ScreenUpdating = True End Sub 

要做到这一点没有时间,并更容易获得条件:

 Option Explicit Sub Hide() Application.ScreenUpdating = False Dim wks As Worksheet Dim Lastrow As String Dim i As Long, hideB As Boolean Dim cell As Variant For Each wks In ThisWorkbook.Worksheets wks.Rows.Hidden = False Lastrow = wks.Range("H" & Rows.Count).End(xlUp).Row If Lastrow > 1 Then cell = wks.Range("H1:H" & Lastrow).Value Set Rng = Nothing: hideB = False For i = 1 To Lastrow If Len(cell(i, 1)) Then 'this determinates if the line will be hidden hideB = (LCase(cell(i, 1)) = "closed") 'if you want to check G also => hideB = (LCase(cell(i, 1)) = "closed") And (LCase(wks.cells(i, "G")) = "keyword2") End If If hideB Then If Rng Is Nothing Then Set Rng = wks.Rows(i) Else Set Rng = Union(Rng, wks.Rows(i)) End If End If Next If Not Rng Is Nothing Then Rng.EntireRow.Hidden = True End If Next MsgBox "All Rows containing Closed in Column H have been hidden", vbInformation, "Information" End Sub 

这是做你想要的吗?

 Sub Hide() Dim wks As Worksheet Dim lastrow As Long Dim startrow As Long Dim i As Long Application.ScreenUpdating = False On Error Resume Next For Each wks In ThisWorkbook.Worksheets With wks .Rows.Hidden = False lastrow = .Range("H" & .Rows.Count).End(xlUp).Row For i = 2 To lastrow If .Cells(i, "H").Value = "Closed" Then startrow = i + 1 Do Until .Cells(i + 1, "H").Value = "Closed" Or i + 1 > lastrow i = i + 1 Loop If .Cells(i + 1, "H").Value = "Closed" Then If i >= startrow Then .Rows(startrow).Resize(i - startrow + 1).Hidden = True i = i + 1 End If End If Next i End With Next wks MsgBox "All Rows containing Closed in Column H have been hidden", vbInformation, "Information" Application.ScreenUpdating = True End Sub