Forum Discussion

Nguyen Giang's avatar
Nguyen Giang
Copper Contributor
Mar 12, 2018

Collapse and Expanding Worksheet Tabs

Dear Excel Community,

 

Is there a way that i can combine worksheets into expandable and collapse-able tab.

 

i.e. i have a total of 12 tabs one for each month and its too many tab so i would like to collapse the first three tab, january, february, and march into a tab called quarter 1, then i do the same for the next 3, april, may, and june into a second tab called quarter 2. and so on and so on.

 

Thank you very much for taking your time to help me with this issue

  • Jamil's avatar
    Jamil
    Mar 13, 2018

    Hello Nguyen,

     

    The link you are referring to, is not going to help you, because that code there is expanding and collapsing all worksheets except two. The loop is Thisworkbook.sheets 

     

    What you need is to have 12 Months worksheets and then 4 sheets each for a single quarter and then you need 4 separate codes to place in each of those quarter sheets.  In Addition, you need one helper worksheet that has to be there all the time and must not be deleted. that worksheet is needed, in case if you collapse all quarters and there has to be a sheet to be activated in order for VBA to avoid tossing error.

     

    I have written the below code that loops through the array of worksheets for specific quarter like this 

    ThisWorkbook.Sheets(Array("Jan", "Feb", "Mar"))

     

    The animation shows how it works.  I have attached the workbook with code embedded to it.

     

     

     

    Untitled-Project51010fd2245a5797.gif

     

     

     

    Private Sub Worksheet_Activate()
    ' This is for first Quarter Sheet
        Dim sheet As Worksheet
    
        Dim sheetsArray As Sheets
        Set sheetsArray = ThisWorkbook.Sheets(Array("Jan", "Feb", "Mar"))
    
    
        Application.ScreenUpdating = False
        If ShowHide1.Name = "Show Quarter1" Then
    
            For Each sheet In sheetsArray
                sheet.Visible = xlSheetVisible
            Next sheet
    
            ShowHide1.Name = "Hide Quarter1"
            ShowHide1.Tab.Color = vbYellow
    
            Sheet1.Activate
        Else
    
            For Each sheet In sheetsArray
                If (sheet.Name <> ShowHide1.Name And sheet.Name <> AlwaysShow.Name) Then
                   sheet.Visible = xlSheetVeryHidden
                End If
            Next sheet
    
            ShowHide1.Name = "Show Quarter1"
            ShowHide1.Tab.Color = vbGreen
    
            AlwaysShow.Activate
        End If
        Application.ScreenUpdating = True
    End Sub
    
    Private Sub Worksheet_Activate()
    ' This is for Second Quarter Sheet
        Dim sheet As Worksheet
    
        Dim sheetsArray As Sheets
        Set sheetsArray = ThisWorkbook.Sheets(Array("Apr", "May", "Jun"))
    
    
        Application.ScreenUpdating = False
        If ShowHide2.Name = "Show Quarter2" Then
    
            For Each sheet In sheetsArray
                sheet.Visible = xlSheetVisible
            Next sheet
    
            ShowHide2.Name = "Hide Quarter2"
             ShowHide2.Tab.Color = vbYellow
    
            Sheet6.Activate
        Else
    
            For Each sheet In sheetsArray
                If (sheet.Name <> ShowHide2.Name And sheet.Name <> AlwaysShow.Name) Then
                   sheet.Visible = xlSheetVeryHidden
                End If
            Next sheet
    
            ShowHide2.Name = "Show Quarter2"
             ShowHide2.Tab.Color = vbGreen
    
            AlwaysShow.Activate
        End If
        Application.ScreenUpdating = True
    End Sub
    
    Private Sub Worksheet_Activate()
    
    ' This is for Third Quarter Sheet
        Dim sheet As Worksheet
    
        Dim sheetsArray As Sheets
        Set sheetsArray = ThisWorkbook.Sheets(Array("Jul", "Aug", "Sep"))
    
    
        Application.ScreenUpdating = False
        If ShowHide3.Name = "Show Quarter3" Then
    
            For Each sheet In sheetsArray
                sheet.Visible = xlSheetVisible
            Next sheet
    
            ShowHide3.Name = "Hide Quarter3"
             ShowHide3.Tab.Color = vbYellow
    
            Sheet10.Activate
        Else
    
            For Each sheet In sheetsArray
                If (sheet.Name <> ShowHide3.Name And sheet.Name <> AlwaysShow.Name) Then
                   sheet.Visible = xlSheetVeryHidden
                End If
            Next sheet
    
            ShowHide3.Name = "Show Quarter3"
            ShowHide3.Tab.Color = vbGreen
    
            AlwaysShow.Activate
        End If
        Application.ScreenUpdating = True
    End Sub
    
    
    Private Sub Worksheet_Activate()
    ' This is for forth Quarter Sheet
        Dim sheet As Worksheet
    
        Dim sheetsArray As Sheets
        Set sheetsArray = ThisWorkbook.Sheets(Array("Oct", "Nov", "Dec"))
    
    
        Application.ScreenUpdating = False
        If ShowHide4.Name = "Show Quarter4" Then
    
            For Each sheet In sheetsArray
                sheet.Visible = xlSheetVisible
            Next sheet
    
            ShowHide4.Name = "Hide Quarter4"
            ShowHide4.Tab.Color = vbYellow
    
            Sheet15.Activate
        Else
    
            For Each sheet In sheetsArray
                If (sheet.Name <> ShowHide4.Name And sheet.Name <> AlwaysShow.Name) Then
                   sheet.Visible = xlSheetVeryHidden
                End If
            Next sheet
    
            ShowHide4.Name = "Show Quarter4"
            ShowHide4.Tab.Color = vbGreen
    
            AlwaysShow.Activate
        End If
        Application.ScreenUpdating = True
    End Sub

     

    • Nguyen Giang's avatar
      Nguyen Giang
      Copper Contributor

      Dear Haytham,

       

      Thank you for your response but the link below is not exactly what i'm looking for.

       

      i want something kinda like this link here.

       

      I'm not sure how to use that myself.

      • Jamil's avatar
        Jamil
        Bronze Contributor

        Hello Nguyen,

         

        The link you are referring to, is not going to help you, because that code there is expanding and collapsing all worksheets except two. The loop is Thisworkbook.sheets 

         

        What you need is to have 12 Months worksheets and then 4 sheets each for a single quarter and then you need 4 separate codes to place in each of those quarter sheets.  In Addition, you need one helper worksheet that has to be there all the time and must not be deleted. that worksheet is needed, in case if you collapse all quarters and there has to be a sheet to be activated in order for VBA to avoid tossing error.

         

        I have written the below code that loops through the array of worksheets for specific quarter like this 

        ThisWorkbook.Sheets(Array("Jan", "Feb", "Mar"))

         

        The animation shows how it works.  I have attached the workbook with code embedded to it.

         

         

         

        Untitled-Project51010fd2245a5797.gif

         

         

         

        Private Sub Worksheet_Activate()
        ' This is for first Quarter Sheet
            Dim sheet As Worksheet
        
            Dim sheetsArray As Sheets
            Set sheetsArray = ThisWorkbook.Sheets(Array("Jan", "Feb", "Mar"))
        
        
            Application.ScreenUpdating = False
            If ShowHide1.Name = "Show Quarter1" Then
        
                For Each sheet In sheetsArray
                    sheet.Visible = xlSheetVisible
                Next sheet
        
                ShowHide1.Name = "Hide Quarter1"
                ShowHide1.Tab.Color = vbYellow
        
                Sheet1.Activate
            Else
        
                For Each sheet In sheetsArray
                    If (sheet.Name <> ShowHide1.Name And sheet.Name <> AlwaysShow.Name) Then
                       sheet.Visible = xlSheetVeryHidden
                    End If
                Next sheet
        
                ShowHide1.Name = "Show Quarter1"
                ShowHide1.Tab.Color = vbGreen
        
                AlwaysShow.Activate
            End If
            Application.ScreenUpdating = True
        End Sub
        
        Private Sub Worksheet_Activate()
        ' This is for Second Quarter Sheet
            Dim sheet As Worksheet
        
            Dim sheetsArray As Sheets
            Set sheetsArray = ThisWorkbook.Sheets(Array("Apr", "May", "Jun"))
        
        
            Application.ScreenUpdating = False
            If ShowHide2.Name = "Show Quarter2" Then
        
                For Each sheet In sheetsArray
                    sheet.Visible = xlSheetVisible
                Next sheet
        
                ShowHide2.Name = "Hide Quarter2"
                 ShowHide2.Tab.Color = vbYellow
        
                Sheet6.Activate
            Else
        
                For Each sheet In sheetsArray
                    If (sheet.Name <> ShowHide2.Name And sheet.Name <> AlwaysShow.Name) Then
                       sheet.Visible = xlSheetVeryHidden
                    End If
                Next sheet
        
                ShowHide2.Name = "Show Quarter2"
                 ShowHide2.Tab.Color = vbGreen
        
                AlwaysShow.Activate
            End If
            Application.ScreenUpdating = True
        End Sub
        
        Private Sub Worksheet_Activate()
        
        ' This is for Third Quarter Sheet
            Dim sheet As Worksheet
        
            Dim sheetsArray As Sheets
            Set sheetsArray = ThisWorkbook.Sheets(Array("Jul", "Aug", "Sep"))
        
        
            Application.ScreenUpdating = False
            If ShowHide3.Name = "Show Quarter3" Then
        
                For Each sheet In sheetsArray
                    sheet.Visible = xlSheetVisible
                Next sheet
        
                ShowHide3.Name = "Hide Quarter3"
                 ShowHide3.Tab.Color = vbYellow
        
                Sheet10.Activate
            Else
        
                For Each sheet In sheetsArray
                    If (sheet.Name <> ShowHide3.Name And sheet.Name <> AlwaysShow.Name) Then
                       sheet.Visible = xlSheetVeryHidden
                    End If
                Next sheet
        
                ShowHide3.Name = "Show Quarter3"
                ShowHide3.Tab.Color = vbGreen
        
                AlwaysShow.Activate
            End If
            Application.ScreenUpdating = True
        End Sub
        
        
        Private Sub Worksheet_Activate()
        ' This is for forth Quarter Sheet
            Dim sheet As Worksheet
        
            Dim sheetsArray As Sheets
            Set sheetsArray = ThisWorkbook.Sheets(Array("Oct", "Nov", "Dec"))
        
        
            Application.ScreenUpdating = False
            If ShowHide4.Name = "Show Quarter4" Then
        
                For Each sheet In sheetsArray
                    sheet.Visible = xlSheetVisible
                Next sheet
        
                ShowHide4.Name = "Hide Quarter4"
                ShowHide4.Tab.Color = vbYellow
        
                Sheet15.Activate
            Else
        
                For Each sheet In sheetsArray
                    If (sheet.Name <> ShowHide4.Name And sheet.Name <> AlwaysShow.Name) Then
                       sheet.Visible = xlSheetVeryHidden
                    End If
                Next sheet
        
                ShowHide4.Name = "Show Quarter4"
                ShowHide4.Tab.Color = vbGreen
        
                AlwaysShow.Activate
            End If
            Application.ScreenUpdating = True
        End Sub

         

  • marianadelgui's avatar
    marianadelgui
    Copper Contributor
    HI Jamil
    I was trying to get this to work for an yearly distribution of sheets, but the error 424 keeps popping up. How do I fix it?

Resources