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

时间:2015-12-16 10:02:22

标签: excel vba excel-vba

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

我的工作表的屏幕截图

Screenshot of my worksheet

这是我的代码:

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

所以这将隐藏任何包含" Closed"在我的第一个工作表中" H"。

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

2 个答案:

答案 0 :(得分:1)

要快速完成,请尝试以下方法:

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

答案 1 :(得分:0)

这样做你想要的吗?

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