Consulting

Results 1 to 4 of 4

Thread: Showing & Hiding Rows based on values

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location

    Showing & Hiding Rows based on values

    Hi,
    I created a toggle button to hide rows based on a value. I found some code for this and it works great. However, I can't make it show rows looping through the hidden rows. Basically, I want to do the opposite as the first part of the code.

    Here is the code:

    Private Sub ToggleButton2_Click()
      Dim rg As Range, c As Range
      Dim firstAddress As String
        Set rg = Range("B4", Cells(Cells.Rows.Count, "B").End(xlUp))
    'Application.ScreenUpdating = False
    
    If ToggleButton2.Value = True Then
    With rg
        Set c = .Find(what:="Complete", lookat:=xlWhole, LookIn:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.EntireRow.Hidden = True
                Set c = .FindNext(c)
                If c Is Nothing Then Exit Do
            Loop While c.Address <> firstAddress
        End If
    Application.ScreenUpdating = True
    End With
        Range("M1").Select
    Else
        Cells.Select
        Selection.EntireRow.Hidden = False
        Range("M1").Select
    End If
    End Sub
    Last edited by Paul_Hossler; 01-21-2019 at 08:14 AM. Reason: Use CODE Tags please

  2. #2
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    In other words, I'm looking for a way to show rows that were previously hidden from the first if statement (based on "Completed" in a certain column). I have other hidden rows that have different values that I do not want shown, so it's only based on the value "Completed" in column B.

    Private Sub ToggleButton2_Click()
      Dim rg As Range, c As Range
      Dim firstAddress As String
        Set rg = Range("B4", Cells(Cells.Rows.Count, "B").End(xlUp))
    'Application.ScreenUpdating = False
    
    If ToggleButton2.Value = True Then
    With rg
        Set c = .Find(what:="Complete", lookat:=xlWhole, LookIn:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.EntireRow.Hidden = True
                Set c = .FindNext(c)
                If c Is Nothing Then Exit Do
            Loop While c.Address <> firstAddress
        End If
    Application.ScreenUpdating = True
    End With
        Range("M1").Select
    Else
    
    '**************************************************************************************************************************
    '  THIS SECTION IS WHERE I'D LIKE SOME HELP.
    '  I'M TRYING TO SHOW ROWS PREVIOUSLY HIDDEN FROM THE CODE ABOVE IN THE IF STATEMENT BUT ONLY FOR "COMPLETED" IN COLUMN B
    
        Cells.Select
        Selection.EntireRow.Hidden = False
        Range("M1").Select
    '***************************************************************************************************************************
    End If
    End Sub

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    If I understood, this fragment should work. Try the attachment with has hidden rows with "Completed" and some hidden rows with something different

    Option Explicit
    
    
    Sub UnhideCompleted()
        Dim r As Range, c As Range
        
        For Each c In Range("B4:B30").Cells
            If c.Value = "Completed" And c.EntireRow.Hidden Then
                c.EntireRow.Hidden = False
            End If
        Next
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Jan 2018
    Location
    Nova Scotia
    Posts
    83
    Location
    Works perfectly. Thank you Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •