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

首先,我的excel工作簿中有一个宏,它根据单元格值隐藏行.这很有效.首先,这是我的工作表的样子:

我的工作表的屏幕截图

这是我的代码:

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”列中包含“已关闭”的任何行.

但正如您在我的打印屏幕上看到的那样,我想隐藏第一个符号值“已关闭”和第二个值“已关闭”之间的所有行.直到最后一行这样做.但我不知道该怎么做,有人可以帮我吗?

最佳答案 要快速完成,请尝试以下方法:

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
点赞