+ Reply to Thread
Results 1 to 1 of 1

[HELP] Macro to Hide/Unhide rows based on Header Styles

Hybrid View

  1. #1
    Registered User
    Join Date
    08-07-2014
    Location
    Sydney, Australia
    MS-Off Ver
    2010
    Posts
    5

    [HELP] Macro to Hide/Unhide rows based on Header Styles

    Hi,

    I'm trying to edit a macro to hide/unhide rows based on header styles.

    What I currently have is a Show and Hide button on every sheet in the workbook that performs the below routine. What the routine does is if every single row is visible and the hide button is pressed once, then it hides everything except Header 1 and Header 2, and if the hide button is pressed the 2nd time, then it hides everything except Header 1. Similarly, it does the same thing when Show button is pressed.

    What I'm trying to achieve if have a Header 3 in there as well. So if every single row is visible and the Hide button is pressed once, then it hides everything except Header 1, Header 2 and Header 3, if the Hide Button is pressed the 2nd time, then hides everything except Header 1 and Header 2 and when pressed the 3rd time, hides everything except Header 1.

    Could someone please help me amend this code, or if there is a better way to execute what I'm trying to achieve, then I'm all ears!

    Thank you for your help!

    'ShowHideMaster is attached to buttons to show/hide all rows bar headers
    'CycleSectionHiddenMaster is attached to the double click function to cycle through visibility options for a section
    'Each of these master macros makes use of the following subroutines and functions:
    'CycleSection performs the hiding/unhiding for a number of "to be" states
    'SectionEndCell is a function to find the end of a section (i.e. the next time the same or higher level header is reached)
    'SectionStatus returns the visibility status of the section, from all hidden to all visible through only headers visible, etc
    
    Sub BSShowHide()
    Dim FirstCell As Range
    Dim IsProtected As Boolean
    
    Const Header1Style As String = "Heading 1"
    Const Header2Style As String = "Heading 2"
    Const FakeStyle As String = "This is a fake style"
    
    
        Application.StatusBar = "Hiding/showing rows: please wait..."
        Application.ScreenUpdating = False
    
        Set FirstCell = Range("B15")
        Do While FirstCell.Style <> Header1Style
            Set FirstCell = FirstCell.Offset(1, 0)
            If FirstCell.Row > Cells.SpecialCells(xlCellTypeLastCell).Row Then
                GoTo TheEnd
            End If
        Loop
    
        If Application.Caller = "ShowButton" Then 'show button pressed
            Application.StatusBar = "Unhiding rows: please wait..."
            If SectionStatus(FirstCell, Cells.SpecialCells(xlCellTypeLastCell).Offset(-1, 0), Header1Style, Header2Style) = 1 Then
                'all hidden except first headers, so go to showing all headers
                Call CycleSection(FirstCell, Cells.SpecialCells(xlCellTypeLastCell), 2, Header1Style, Header2Style)
            Else
                'any other status, go to showing everything
                Call CycleSection(FirstCell, Cells.SpecialCells(xlCellTypeLastCell), 3, Header1Style, Header2Style)
            End If
        ElseIf Application.Caller = "HideButton" Then 'hide button pressed
            Application.StatusBar = "Hiding rows: please wait..."
            If SectionStatus(FirstCell, Cells.SpecialCells(xlCellTypeLastCell).Offset(-1, 0), Header1Style, Header2Style) = 4 Then
                'everything showing, so go to showing all headers
                Call CycleSection(FirstCell, Cells.SpecialCells(xlCellTypeLastCell), 2, Header1Style, Header2Style)
            Else
                'any other status, go to showing only first headers (by using a fake colour for second headers)
                Call CycleSection(FirstCell, Cells.SpecialCells(xlCellTypeLastCell), 2, Header1Style, FakeStyle)
            End If
        Else
            'procedure called not using buttons
            MsgBox "Invalid caller"
        End If
    
    TheEnd:
        Application.ScreenUpdating = True
        Application.StatusBar = False
        Exit Sub
    
    
    End Sub
    
    Sub CycleSection(FirstCell As Range, LastCell As Range, NewStatus As Integer, Header1Style As String, Header2Style As String)
    'cycles hidden/visible status of section
    'NewStatus=1 hides everything
    'NewStatus=2 hides everything except all section headers
    'NewStatus=3 unhides everything
    Dim VisibleRange As Range
    Dim TheRow As Long
    Dim LowerHeadersFound As Boolean
    
        Select Case NewStatus
            Case 1 'hide everything
                Range(FirstCell.Offset(1, 0), LastCell).EntireRow.Hidden = True
                FirstCell.EntireRow.Hidden = False
            Case 2 'unhide all lower section headers
                LowerHeadersFound = False
                Set VisibleRange = FirstCell.EntireRow
                'find section headers
                For TheRow = FirstCell.Offset(1, 0).Row To LastCell.Row
                    If Cells(TheRow, 2).Style = Header2Style Or Cells(TheRow, 2).Style = Header1Style Then
                        LowerHeadersFound = True
                        Set VisibleRange = Union(VisibleRange, Cells(TheRow, 2).EntireRow)
                    End If
                Next TheRow
                If LowerHeadersFound = False Then 'no subheaders
                    Range(FirstCell, LastCell).EntireRow.Hidden = False
                Else 'subheaders exist
                    Range(FirstCell.Offset(1, 0), LastCell).EntireRow.Hidden = True
                    VisibleRange.EntireRow.Hidden = False
                End If
            Case 3
                Range(FirstCell, LastCell).EntireRow.Hidden = False
            Case Else
                'do nothing if something else has been passed
        End Select
        
    
    End Sub
    
    
    Function SectionStatus(FirstCell As Range, LastCell As Range, Header1Style As String, Header2Style As String) As Integer
    'sectionstatus=0 if not a section header
    'sectionstatus=1 if everything is hidden except top level headers
    'sectionstatus=2 if only and all headers are showing
    'sectionstatus=3 if anything else...
    'sectionstatus=4 if everything is visible
        
    Dim SectionComplete As Boolean
    Dim UpperLevelHiddenFound As Boolean
    Dim UpperLevelVisibleFound As Boolean
    Dim LowerLevelHiddenFound As Boolean
    Dim LowerLevelVisibleFound As Boolean
    Dim NoHeaderHiddenFound As Boolean
    Dim NoHeaderVisibleFound As Boolean
    Dim Counter As Long
    Dim FirstCellStyle As String
    Dim TheCell As Range 'index cell
    
        'set up variables
        FirstCellStyle = FirstCell.Style
        SectionComplete = False
        LowerLevelHiddenFound = False
        LowerLevelVisibleFound = False
        NoHeaderHiddenFound = False
        NoHeaderVisibleFound = False
    
        If FirstCellStyle <> Header1Style And FirstCellStyle <> Header2Style Then
            'initial cell is not a header
            SectionStatus = 0
        ElseIf FirstCell.Row = LastCell.Row Then
            'no section
            SectionStatus = 0
        Else
            'runs down rows to ascertain what is in the section
            Counter = 1
            Do While SectionComplete = False
                Set TheCell = FirstCell.Offset(Counter)
                If TheCell.Row > LastCell.Row Then
                    SectionComplete = True
                ElseIf TheCell.Style = Header1Style Then
                    If TheCell.EntireRow.Hidden = True Then
                        UpperLevelHiddenFound = True
                    ElseIf TheCell.EntireRow.Hidden = False Then
                        UpperLevelVisibleFound = True
                    End If
                ElseIf TheCell.Style = Header2Style Then
                    If TheCell.EntireRow.Hidden = True Then
                        LowerLevelHiddenFound = True
                    ElseIf TheCell.EntireRow.Hidden = False Then
                        LowerLevelVisibleFound = True
                    End If
                Else
                    If TheCell.EntireRow.Hidden = True Then
                        NoHeaderHiddenFound = True
                    ElseIf TheCell.EntireRow.Hidden = False Then
                        NoHeaderVisibleFound = True
                    End If
                End If
                Counter = Counter + 1
            Loop
        
            'determines status by what has been found in the section
            If NoHeaderVisibleFound = False And LowerLevelHiddenFound = False And UpperLevelHiddenFound = False Then
                SectionStatus = 2
            ElseIf NoHeaderVisibleFound = False And LowerLevelVisibleFound = False Then
                SectionStatus = 1
            ElseIf NoHeaderHiddenFound = False And LowerLevelHiddenFound = False And UpperLevelHiddenFound = False Then
                SectionStatus = 4
            Else
                SectionStatus = 3
            End If
    
        End If
    
    End Function
    
    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    'End of Show/Hidefunctionality
    '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro to unhide & hide rows based on column value
    By qvfr034 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-15-2020, 03:00 PM
  2. Macro for Hide Or Unhide Rows based on Cell Text
    By Neilesh Kumar in forum Excel General
    Replies: 3
    Last Post: 04-14-2017, 08:36 AM
  3. Macro to Hide/Unhide Rows Based on Cell Value
    By mlancaster24 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-07-2015, 01:20 PM
  4. [SOLVED] Macro to Hide/Unhide rows based on the result of a formula
    By c850b in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-20-2015, 12:52 PM
  5. [SOLVED] Macro to Automatically Hide/Unhide Rows Based on Checkbox
    By bga10s in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-27-2014, 07:32 PM
  6. simple macro to hide or unhide rows based on a cell value
    By Russ Fuquay in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-19-2013, 06:32 PM
  7. Macro to Hide/Unhide Multiple Rows Based on Cell Value
    By mbp727 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2013, 11:12 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1