SOLVED
Home

Collapse and Expanding Worksheet Tabs

%3CLINGO-SUB%20id%3D%22lingo-sub-170866%22%20slang%3D%22en-US%22%3ECollapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-170866%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20Excel%20Community%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIs%20there%20a%20way%20that%20i%20can%20combine%20worksheets%20into%20expandable%20and%20collapse-able%20tab.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ei.e.%20i%20have%20a%20total%20of%2012%20tabs%20one%20for%20each%20month%20and%20its%20too%20many%20tab%20so%20i%20would%20like%20to%20collapse%20the%20first%20three%20tab%2C%20january%2C%20february%2C%20and%20march%20into%20a%20tab%20called%20quarter%201%2C%20then%20i%20do%20the%20same%20for%20the%20next%203%2C%20april%2C%20may%2C%20and%20june%20into%20a%20second%20tab%20called%20quarter%202.%20and%20so%20on%20and%20so%20on.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThank%20you%20very%20much%20for%20taking%20your%20time%20to%20help%20me%20with%20this%20issue%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-170866%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-353534%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-353534%22%20slang%3D%22en-US%22%3EYou%20are%20very%20welcome.%20Thanks%20for%20feedback%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-339706%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-339706%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20so%20much.%20that%20helped%20a%20ton.%20I%20figured%20out%20the%20names%20for%20my%20sheets%20in%20VB%20did%20not%20match.%20Thanks%20a%20ton.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-339627%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-339627%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Rusty%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eplease%20see%20attached%20workbook.%26nbsp%3B%20I%20have%20modified%20the%20workbook%20as%20per%20your%20need.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-339471%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-339471%22%20slang%3D%22en-US%22%3E%3CP%3EPrivate%20Sub%20Worksheet_Activate()%3CBR%20%2F%3E'%20This%20is%20for%20first%20Quarter%20Sheet%3CBR%20%2F%3EDim%20sheet%20As%20Worksheet%3C%2FP%3E%3CP%3EDim%20sheetsArray%20As%20Sheets%3CBR%20%2F%3ESet%20sheetsArray%20%3D%20ThisWorkbook.Sheets(Array(%22MONDAY%202.4.19%22%2C%20%22TUESDAY%202.5.19%22%2C%20%22WEDNESDAY%202.6.19%22%2C%20%22THURSDAY%202.7.19%22%2C%20%22FRIDAY%202.8.19%22%2C%20%22SATURDAY%202.9.19%22))%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EApplication.ScreenUpdating%20%3D%20False%3CBR%20%2F%3EIf%20ShowHide1.Name%20%3D%20%22TOTALS%202.4%20-%202.9.19%22%20Then%3C%2FP%3E%3CP%3EFor%20Each%20sheet%20In%20sheetsArray%3CBR%20%2F%3Esheet.Visible%20%3D%20xlSheetVisible%3CBR%20%2F%3ENext%20sheet%3C%2FP%3E%3CP%3EShowHide1.Name%20%3D%20%22TOTALS%202.4%20-%202.9.19%22%3CBR%20%2F%3EShowHide1.Tab.Color%20%3D%20vbYellow%3C%2FP%3E%3CP%3ESheet1.Activate%3CBR%20%2F%3EElse%3C%2FP%3E%3CP%3EFor%20Each%20sheet%20In%20sheetsArray%3CBR%20%2F%3EIf%20(sheet.Name%20%26lt%3B%26gt%3B%20ShowHide1.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20AlwaysShow.Name)%20Then%3CBR%20%2F%3Esheet.Visible%20%3D%20xlSheetVeryHidden%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3ENext%20sheet%3C%2FP%3E%3CP%3EShowHide1.Name%20%3D%20%22TOTALS%202.4%20-%202.9.19%22%3CBR%20%2F%3EShowHide1.Tab.Color%20%3D%20vbGreen%3C%2FP%3E%3CP%3EAlwaysShow.Activate%3CBR%20%2F%3EEnd%20If%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20use%20the%20above%20code%20to%20collapse%20a%20weeks%20worth%20of%20sheets.%20It%20stops%20on%20If.%20showhide.name.%26nbsp%3B%3C%2FP%3E%3CP%3ESays%20Identifier%20under%20cursor%20is%20not%20defined.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-171250%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-171250%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Nguyen%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20link%20you%20are%20referring%20to%2C%20is%20not%20going%20to%20help%20you%2C%20because%20that%20code%20there%20is%20expanding%20and%20collapsing%20all%20worksheets%20except%20two.%20The%20loop%20is%20Thisworkbook.sheets%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20you%20need%20is%20to%20have%2012%20Months%20worksheets%20and%20then%204%20sheets%20each%20for%20a%20single%20quarter%20and%20then%20you%20need%204%20separate%20codes%20to%20place%20in%20each%20of%20those%20quarter%20sheets.%26nbsp%3B%20In%20Addition%2C%20you%20need%20one%20helper%20worksheet%20that%20has%20to%20be%20there%20all%20the%20time%20and%20must%20not%20be%20deleted.%20that%20worksheet%20is%20needed%2C%20in%20case%20if%20you%20collapse%20all%20quarters%20and%20there%20has%20to%20be%20a%20sheet%20to%20be%20activated%20in%20order%20for%20VBA%20to%20avoid%20tossing%20error.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20have%20written%20the%20below%20code%20that%20loops%20through%20the%20array%20of%20worksheets%20for%20specific%20quarter%20like%20this%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThisWorkbook.Sheets(Array(%22Jan%22%2C%20%22Feb%22%2C%20%22Mar%22))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20animation%20shows%20how%20it%20works.%26nbsp%3B%20I%20have%20attached%20the%20workbook%20with%20code%20embedded%20to%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgifyu.com%2Fimage%2Fssj4%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fs1.gifyu.com%2Fimages%2FUntitled-Project51010fd2245a5797.gif%22%20border%3D%220%22%20alt%3D%22Untitled-Project51010fd2245a5797.gif%22%20%2F%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPrivate%20Sub%20Worksheet_Activate()%0A'%20This%20is%20for%20first%20Quarter%20Sheet%0A%20%20%20%20Dim%20sheet%20As%20Worksheet%0A%0A%20%20%20%20Dim%20sheetsArray%20As%20Sheets%0A%20%20%20%20Set%20sheetsArray%20%3D%20ThisWorkbook.Sheets(Array(%22Jan%22%2C%20%22Feb%22%2C%20%22Mar%22))%0A%0A%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20If%20ShowHide1.Name%20%3D%20%22Show%20Quarter1%22%20Then%0A%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVisible%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A%20%20%20%20%20%20%20%20ShowHide1.Name%20%3D%20%22Hide%20Quarter1%22%0A%20%20%20%20%20%20%20%20ShowHide1.Tab.Color%20%3D%20vbYellow%0A%0A%20%20%20%20%20%20%20%20Sheet1.Activate%0A%20%20%20%20Else%0A%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20(sheet.Name%20%26lt%3B%26gt%3B%20ShowHide1.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20AlwaysShow.Name)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVeryHidden%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A%20%20%20%20%20%20%20%20ShowHide1.Name%20%3D%20%22Show%20Quarter1%22%0A%20%20%20%20%20%20%20%20ShowHide1.Tab.Color%20%3D%20vbGreen%0A%0A%20%20%20%20%20%20%20%20AlwaysShow.Activate%0A%20%20%20%20End%20If%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%0A%0APrivate%20Sub%20Worksheet_Activate()%0A'%20This%20is%20for%20Second%20Quarter%20Sheet%0A%20%20%20%20Dim%20sheet%20As%20Worksheet%0A%0A%20%20%20%20Dim%20sheetsArray%20As%20Sheets%0A%20%20%20%20Set%20sheetsArray%20%3D%20ThisWorkbook.Sheets(Array(%22Apr%22%2C%20%22May%22%2C%20%22Jun%22))%0A%0A%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20If%20ShowHide2.Name%20%3D%20%22Show%20Quarter2%22%20Then%0A%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVisible%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A%20%20%20%20%20%20%20%20ShowHide2.Name%20%3D%20%22Hide%20Quarter2%22%0A%20%20%20%20%20%20%20%20%20ShowHide2.Tab.Color%20%3D%20vbYellow%0A%0A%20%20%20%20%20%20%20%20Sheet6.Activate%0A%20%20%20%20Else%0A%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20(sheet.Name%20%26lt%3B%26gt%3B%20ShowHide2.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20AlwaysShow.Name)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVeryHidden%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A%20%20%20%20%20%20%20%20ShowHide2.Name%20%3D%20%22Show%20Quarter2%22%0A%20%20%20%20%20%20%20%20%20ShowHide2.Tab.Color%20%3D%20vbGreen%0A%0A%20%20%20%20%20%20%20%20AlwaysShow.Activate%0A%20%20%20%20End%20If%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%0A%0APrivate%20Sub%20Worksheet_Activate()%0A%0A'%20This%20is%20for%20Third%20Quarter%20Sheet%0A%20%20%20%20Dim%20sheet%20As%20Worksheet%0A%0A%20%20%20%20Dim%20sheetsArray%20As%20Sheets%0A%20%20%20%20Set%20sheetsArray%20%3D%20ThisWorkbook.Sheets(Array(%22Jul%22%2C%20%22Aug%22%2C%20%22Sep%22))%0A%0A%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20If%20ShowHide3.Name%20%3D%20%22Show%20Quarter3%22%20Then%0A%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVisible%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A%20%20%20%20%20%20%20%20ShowHide3.Name%20%3D%20%22Hide%20Quarter3%22%0A%20%20%20%20%20%20%20%20%20ShowHide3.Tab.Color%20%3D%20vbYellow%0A%0A%20%20%20%20%20%20%20%20Sheet10.Activate%0A%20%20%20%20Else%0A%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20(sheet.Name%20%26lt%3B%26gt%3B%20ShowHide3.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20AlwaysShow.Name)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVeryHidden%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A%20%20%20%20%20%20%20%20ShowHide3.Name%20%3D%20%22Show%20Quarter3%22%0A%20%20%20%20%20%20%20%20ShowHide3.Tab.Color%20%3D%20vbGreen%0A%0A%20%20%20%20%20%20%20%20AlwaysShow.Activate%0A%20%20%20%20End%20If%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%0A%0A%0APrivate%20Sub%20Worksheet_Activate()%0A'%20This%20is%20for%20forth%20Quarter%20Sheet%0A%20%20%20%20Dim%20sheet%20As%20Worksheet%0A%0A%20%20%20%20Dim%20sheetsArray%20As%20Sheets%0A%20%20%20%20Set%20sheetsArray%20%3D%20ThisWorkbook.Sheets(Array(%22Oct%22%2C%20%22Nov%22%2C%20%22Dec%22))%0A%0A%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20If%20ShowHide4.Name%20%3D%20%22Show%20Quarter4%22%20Then%0A%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVisible%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A%20%20%20%20%20%20%20%20ShowHide4.Name%20%3D%20%22Hide%20Quarter4%22%0A%20%20%20%20%20%20%20%20ShowHide4.Tab.Color%20%3D%20vbYellow%0A%0A%20%20%20%20%20%20%20%20Sheet15.Activate%0A%20%20%20%20Else%0A%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20(sheet.Name%20%26lt%3B%26gt%3B%20ShowHide4.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20AlwaysShow.Name)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVeryHidden%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A%20%20%20%20%20%20%20%20ShowHide4.Name%20%3D%20%22Show%20Quarter4%22%0A%20%20%20%20%20%20%20%20ShowHide4.Tab.Color%20%3D%20vbGreen%0A%0A%20%20%20%20%20%20%20%20AlwaysShow.Activate%0A%20%20%20%20End%20If%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-171201%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-171201%22%20slang%3D%22en-US%22%3E%3CP%3EDear%20Haytham%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThank%20you%20for%20your%20response%20but%20the%20link%20below%20is%20not%20exactly%20what%20i'm%20looking%20for.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ei%20want%20something%20kinda%20like%20this%20%3CA%20href%3D%22https%3A%2F%2Fwww.experts-exchange.com%2Farticles%2F1685%2FExcel-Using-a-Sheet-Tab-as-a-Button-for-Expanding-Collapsing-Supplementary-Sheets.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Elink%3C%2FA%3E%20here.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI'm%20not%20sure%20how%20to%20use%20that%20myself.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-171050%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-171050%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPlease%20check%20this%20%3CA%20href%3D%22https%3A%2F%2Fwww.excelforum.com%2Fexcel-programming-vba-macros%2F1077979-collapse-expand-tabs-in-excel.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Elink%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3EIt%20may%20help%20you!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHope%20that%20helps%3C%2FP%3E%0A%3CP%3EHaytham%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-415835%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-415835%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Jamil%20-%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F24499%22%20target%3D%22_blank%22%3E%40Jamil%20Mohammad%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20for%20the%20sample%20workbook.%20Can%20I%20ask%20as%20a%20complete%20novice%2C%20how%20to%20activate%20the%20code%20(i.e.%20how%20do%20I%20hide%20the%20sheets%20in%20your%20workbook)%3F%3F%3CBR%20%2F%3E%3CBR%20%2F%3EI'm%20not%20sure%20if%20I%20was%20expecting%20to%20see%20a%20button%20or%20option%20selector%2C%20but%20I%20can't%20figure%20out%20how%20to%20make%20it%20work!%3CBR%20%2F%3E%3CBR%20%2F%3EThank%20you%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-416971%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-416971%22%20slang%3D%22en-US%22%3EHi%20Angela%2C%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20code%20is%20activated%20automatically%20using%20worksheet%20even%20handler.%20%3CBR%20%2F%3E%3CBR%20%2F%3Eit%20is%20important%20to%20modify%20the%20sheet%20names%20in%20the%20code%20to%20make%20it%20work.%20If%20you%20post%20a%20sample%20workbook.%20I%20can%20modify%20the%20code%20and%20share%20the%20file%20with%20you.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-445346%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-445346%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F24499%22%20target%3D%22_blank%22%3E%40Jamil%20Mohammad%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20would%20be%20very%20kind%2C%20thank%20you.%3C%2FP%3E%3CP%3EAttached%20is%20a%20sample%20workbook%20-%20I'd%20like%20the%20first%202%20tabs%20to%20be%20visible%20at%20all%20times.%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20tab%20named%20'Inv1'%20has%203%20sub%20tabs%20which%20I'd%20like%20to%20be%20able%20to%20either%20expand%20or%20collapse.%3C%2FP%3E%3CP%3ESimilarly%20with%20the%20tab%20named%20'Inv2'.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eangela%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-445363%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-445363%22%20slang%3D%22en-US%22%3E%3CP%3Esorry%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F24499%22%20target%3D%22_blank%22%3E%40Jamil%20Mohammad%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%20here%20is%20the%20file!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-446352%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-446352%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F109877%22%20target%3D%22_blank%22%3E%40Angela%20McGhin%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPlease%20see%20attached.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-452578%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-452578%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F24499%22%20target%3D%22_blank%22%3E%40Jamil%20Mohammad%3C%2FA%3EThat%20is%20fantastic%2C%20Jamil!!%3CBR%20%2F%3EThank%20you%20so%20much.%20You've%20saved%20me%20hours%20of%20trying%20to%20figure%20it%20out!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-719500%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-719500%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F24499%22%20target%3D%22_blank%22%3E%40Jamil%20Mohammad%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20so%20much%20for%20the%20sample%20workbook%20you%20created.%3C%2FP%3E%3CP%3EI've%20used%20this%20a%20few%20times%20and%20have%20managed%20to%20get%20familiar%20with%20the%20code%20enough%20to%20be%20able%20to%20adapt%20it%20for%20other%20workbooks%20too.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHowever%20the%20one%20part%20I%20don't%20understand%20is%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CBLOCKQUOTE%3E%3CP%3EElse%3C%2FP%3E%3CP%3EFor%20Each%20sheet%20In%20sheetsArray%3CBR%20%2F%3EIf%20(sheet.Name%20%26lt%3B%26gt%3B%20Sheet6.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20Sheet1.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20Sheet10.Name)%20Then%3CBR%20%2F%3Esheet.Visible%20%3D%20xlSheetVeryHidden%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3ENext%20sheet%3C%2FP%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20somehow%20managed%20to%20adapt%20it%20each%20time%20and%20it%20appears%20to%20be%20working%2C%20but%20can%20you%20explain%20in%20basic%20terms%20what%20this%20part%20of%20the%20code%20does%20so%20I%20can%20understand%20it%20better%2C%20in%20case%20I%20need%20to%20change%20it%20in%20future%3F%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you!!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAngela%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-719588%22%20slang%3D%22en-US%22%3ERe%3A%20Collapse%20and%20Expanding%20Worksheet%20Tabs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-719588%22%20slang%3D%22en-US%22%3E%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F109877%22%20target%3D%22_blank%22%3E%40Angela%20McGhin%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EYou%20are%20welcome!%26nbsp%3B%20thanks%20for%20your%20feedback.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EI%20understand%20that%20why%20it%20is%20confusing.%20I%20will%20try%20to%20explain.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EExcel%20sheets%20have%20two%20alias%26nbsp%3B%20what%20is%20the%20commonly%20used%20worksheet%20name%20and%20the%20other%20is%20the%20%22Code%20Name%22%26nbsp%3B%20%26nbsp%3B%20for%20more%20detail%2C%20i%20suggest%20you%20read%20this%20article%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fwww.spreadsheet1.com%2Fvba-codenames.html%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.spreadsheet1.com%2Fvba-codenames.html%3C%2FA%3E%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EI%20used%20code%20names%20because%20if%20you%20rename%20the%20worksheet%2C%20the%20code%20will%20still%20work.%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EThe%20workbook%20only%20has%20VBA%20in%20two%20worksheet%20models%20which%20its%20code%20names%20are%20sheet3%20and%20sheet6.%26nbsp%3B%20the%20codes%20are%20placed%20in%20the%20worksheet%20model%20because%20they%20are%20event%20driven%20(meaning%20the%20VBA%20will%20run%20if%20an%20a%20preset%20action%20is%20triggered)%20for%20example%20if%20you%20activate%20the%20workbook%2C%20it%20will%20expand%20and%20on%20next%20activation%20it%20will%20collapse.%20This%20is%20what%20means%20by%20event%20driven.%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3ESo%20to%20understand%20the%20code%20fully.%20we%20have%20to%20look%20at%20each%20step%20of%20the%20code.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3EI%20am%20commenting%20it%20here%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPrivate%20Sub%20Worksheet_Activate()%0A%20%20%20%20Dim%20sheet%20As%20Worksheet%0A%0A%20%20%20%20Dim%20sheetsArray%20As%20Sheets%20%20'%20declares%20sheetsarray%20as%20sheets%20which%20is%20sheets%20is%20an%20object%0A%20%20%20%20%0A%20%20%20%20'then%20assigns%20array%20of%20the%20three%20sheet%20names%0A%20%20%20%20Set%20sheetsArray%20%3D%20ThisWorkbook.Sheets(Array(%22Inv2%20TS%22%2C%20%22Inv2%20Exp%22%2C%20%22Inv2%20Sum%22))%0A%20%20%20%20%0A%0A%20%20%20%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20'%20this%20one%20first%20check%20if%20the%20name%20is%20%22show%22%0A%20%20%20%20If%20Sheet6.Name%20%3D%20%22Show%20Inv2%22%20Then%0A%0A'%20if%20the%20abovementioned%20condition%20was%20true%2C%20%20then%20below%20code%20will%20loop%20only%20in%20the%20sheets%20Inv2%20TS%22%2C%20%22Inv2%20Exp%22%2C%20%22Inv2%20Sum%0A'%20and%20set%20them%20visible%22unhide)%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVisible%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A'%20here%20it%20changes%20the%20name%20of%20then%20worksheet%20to%20Hide%20Inv2%20and%20color%20to%20yellow%0A%20%20%20%20%20%20%20%20Sheet6.Name%20%3D%20%22Hide%20Inv2%22%0A%20%20%20%20%20%20%20%20Sheet6.Tab.Color%20%3D%20vbYellow%0A%0A'activates%20the%20first%20sheet%20which%20is%20the%20invoice%20summary%0A%20%20%20%20%20%20%20%20Sheet10.Activate%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20'%20now%20the%20above%20action%20will%20run%20if%20the%20sheet6%20name%20was%20%22Show%20Inv2%22%20%20but%0A%20%20%20%20%20%20%20%20'if%20it%20is%20Not%20Show%20Inv2%20then%20here%20the%20Else%20statement%20will%20run%20the%20below%20code%0A%20%20%20%20Else%0A%0A%20'%20this%20will%20loop%20through%20sheet%20array%20which%20are%20%22Inv2%20TS%22%2C%20%22Inv2%20Exp%22%2C%20%22Inv2%20Sum%22%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20'%20now%20this%20condition%20is%20checking%20if%20that%20it%20only%20should%20affect%20sheets%20that%20its%20name%20are%20not%20Sheet6%20and%20Sheet1%20and%20sheet10%0A%20%20%20%20%20%20%20%20'%20you%20can%20look%20there%20which%20sheets%20codes%20name%20are%206%2C%201%2C%2010%0A%20%20%20%20%20%20%20%20'then%20if%20the%20condition%20met%2C%20and%20exlcuded%206%2C%201%2C%2010%20then%20hide%20those%20sheets%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20If%20(sheet.Name%20%26lt%3B%26gt%3B%20Sheet6.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20Sheet1.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20Sheet10.Name)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVeryHidden%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Next%20sheet%0A'%20then%20set%20a%20new%20name%20which%20is%20show%20Inv2%20to%20the%20sheet6%20name%20and%20also%20change%20color%20to%20green.%0A%20%20%20%20%20%20%20%20Sheet6.Name%20%3D%20%22Show%20Inv2%22%0A%20%20%20%20%20%20%20%20Sheet6.Tab.Color%20%3D%20vbGreen%0A%0A%20%20%20%20%20%20%20%20Sheet10.Activate%0A%20%20%20%20End%20If%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%0A%3C%2FPRE%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3Eis%20important%20to%20mentioned%20here%20that%20the%20code%20can%20be%20simplified%20as%20below.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3Esince%20we%20are%20running%20the%20code%20into%20an%20array%20of%20three%20sheets%20%22Inv2%20TS%22%2C%20%22Inv2%20Exp%22%2C%20%22Inv2%20Sum%22%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3Ethen%20it%20is%20unnecessary%20to%20have%20this%20condition%3C%2FP%3E%0A%3CPRE%3EIf%20(sheet.Name%20%26lt%3B%26gt%3B%20Sheet6.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20Sheet1.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20Sheet10.Name)%20Then%0AEnd%20If%26nbsp%3B%3C%2FPRE%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3Eso%20in%20the%20code%20below%2C%20i%20removed%20the%26nbsp%3BIf%20(sheet.Name%20%26lt%3B%26gt%3B%20Sheet6.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20Sheet1.Name%20And%20sheet.Name%20%26lt%3B%26gt%3B%20Sheet10.Name)%20Then%26nbsp%3B%20%26nbsp%3Balong%20with%20it%20closing%20End%20If%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20justify%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPrivate%20Sub%20Worksheet_Activate()%0A%20%20%20%20Dim%20sheet%20As%20Worksheet%0A%0A%20%20%20%20Dim%20sheetsArray%20As%20Sheets%20%20'%20declares%20sheetsarray%20as%20sheets%20which%20is%20sheets%20is%20an%20object%0A%20%20%20%20%0A%20%20%20%20'then%20assigns%20array%20of%20the%20three%20sheet%20names%0A%20%20%20%20Set%20sheetsArray%20%3D%20ThisWorkbook.Sheets(Array(%22Inv2%20TS%22%2C%20%22Inv2%20Exp%22%2C%20%22Inv2%20Sum%22))%0A%20%20%20%20%0A%0A%20%20%20%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20'%20this%20one%20first%20check%20if%20the%20name%20is%20%22show%22%0A%20%20%20%20If%20Sheet6.Name%20%3D%20%22Show%20Inv2%22%20Then%0A%0A'%20if%20the%20abovementioned%20condition%20was%20true%2C%20%20then%20below%20code%20will%20loop%20only%20in%20the%20sheets%20Inv2%20TS%22%2C%20%22Inv2%20Exp%22%2C%20%22Inv2%20Sum%0A'%20and%20set%20them%20visible%22unhide)%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVisible%0A%20%20%20%20%20%20%20%20Next%20sheet%0A%0A'%20here%20it%20changes%20the%20name%20of%20then%20worksheet%20to%20Hide%20Inv2%20and%20color%20to%20yellow%0A%20%20%20%20%20%20%20%20Sheet6.Name%20%3D%20%22Hide%20Inv2%22%0A%20%20%20%20%20%20%20%20Sheet6.Tab.Color%20%3D%20vbYellow%0A%0A'activates%20the%20first%20sheet%20which%20is%20the%20invoice%20summary%0A%20%20%20%20%20%20%20%20Sheet10.Activate%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20'%20now%20the%20above%20action%20will%20run%20if%20the%20sheet6%20name%20was%20%22Show%20Inv2%22%20%20but%0A%20%20%20%20%20%20%20%20'if%20it%20is%20Not%20Show%20Inv2%20then%20here%20the%20Else%20statement%20will%20run%20the%20below%20code%0A%20%20%20%20Else%0A'code%20will%20loop%20only%20in%20the%20sheets%20Inv2%20TS%22%2C%20%22Inv2%20Exp%22%2C%20%22Inv2%20Sum%0A'%20and%20set%20them%20visible%22unhide)%0A%20%20%20%20%20%20%20%20For%20Each%20sheet%20In%20sheetsArray%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sheet.Visible%20%3D%20xlSheetVeryHidden%0A%20%20%20%20%20%20%20%20Next%20sheet%0A'%20then%20set%20a%20new%20name%20which%20is%20show%20Inv2%20to%20the%20sheet6%20name%20and%20also%20change%20color%20to%20green.%0A%20%20%20%20%20%20%20%20Sheet6.Name%20%3D%20%22Show%20Inv2%22%0A%20%20%20%20%20%20%20%20Sheet6.Tab.Color%20%3D%20vbGreen%0A%0A%20%20%20%20%20%20%20%20Sheet10.Activate%0A%20%20%20%20End%20If%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%0A%0A%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Nguyen Giang
Occasional Contributor

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

15 Replies

Hi,

 

Please check this link.

It may help you!

 

Hope that helps

Haytham

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.

Solution

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

 

Private Sub Worksheet_Activate()
' This is for first Quarter Sheet
Dim sheet As Worksheet

Dim sheetsArray As Sheets
Set sheetsArray = ThisWorkbook.Sheets(Array("MONDAY 2.4.19", "TUESDAY 2.5.19", "WEDNESDAY 2.6.19", "THURSDAY 2.7.19", "FRIDAY 2.8.19", "SATURDAY 2.9.19"))


Application.ScreenUpdating = False
If ShowHide1.Name = "TOTALS 2.4 - 2.9.19" Then

For Each sheet In sheetsArray
sheet.Visible = xlSheetVisible
Next sheet

ShowHide1.Name = "TOTALS 2.4 - 2.9.19"
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 = "TOTALS 2.4 - 2.9.19"
ShowHide1.Tab.Color = vbGreen

AlwaysShow.Activate
End If

 

I am trying to use the above code to collapse a weeks worth of sheets. It stops on If. showhide.name. 

Says Identifier under cursor is not defined. 

Hi Rusty,

 

please see attached workbook.  I have modified the workbook as per your need.

Thanks so much. that helped a ton. I figured out the names for my sheets in VB did not match. Thanks a ton. 

You are very welcome. Thanks for feedback

Hi Jamil -@Jamil Mohammad 

 

Thanks for the sample workbook. Can I ask as a complete novice, how to activate the code (i.e. how do I hide the sheets in your workbook)??

I'm not sure if I was expecting to see a button or option selector, but I can't figure out how to make it work!

Thank you

Hi Angela,

The code is activated automatically using worksheet even handler.

it is important to modify the sheet names in the code to make it work. If you post a sample workbook. I can modify the code and share the file with you.

Hi @Jamil Mohammad 

 

That would be very kind, thank you.

Attached is a sample workbook - I'd like the first 2 tabs to be visible at all times. 

The tab named 'Inv1' has 3 sub tabs which I'd like to be able to either expand or collapse.

Similarly with the tab named 'Inv2'.

 

Any help would be appreciated.

 

angela 

@Jamil MohammadThat is fantastic, Jamil!!
Thank you so much. You've saved me hours of trying to figure it out!

Hi@Jamil Mohammad 

 

Thanks so much for the sample workbook you created.

I've used this a few times and have managed to get familiar with the code enough to be able to adapt it for other workbooks too.

 

However the one part I don't understand is this:

 

Else

For Each sheet In sheetsArray
If (sheet.Name <> Sheet6.Name And sheet.Name <> Sheet1.Name And sheet.Name <> Sheet10.Name) Then
sheet.Visible = xlSheetVeryHidden
End If
Next sheet

 

I've somehow managed to adapt it each time and it appears to be working, but can you explain in basic terms what this part of the code does so I can understand it better, in case I need to change it in future??

 

Thank you!!

 

Angela

@Angela McGhin 

 

You are welcome!  thanks for your feedback.

 

I understand that why it is confusing. I will try to explain.

 

Excel sheets have two alias  what is the commonly used worksheet name and the other is the "Code Name"    for more detail, i suggest you read this article https://www.spreadsheet1.com/vba-codenames.html

 

I used code names because if you rename the worksheet, the code will still work. 

 

The workbook only has VBA in two worksheet models which its code names are sheet3 and sheet6.  the codes are placed in the worksheet model because they are event driven (meaning the VBA will run if an a preset action is triggered) for example if you activate the workbook, it will expand and on next activation it will collapse. This is what means by event driven. 

 

So to understand the code fully. we have to look at each step of the code.

 

I am commenting it here

 

Private Sub Worksheet_Activate()
    Dim sheet As Worksheet

    Dim sheetsArray As Sheets  ' declares sheetsarray as sheets which is sheets is an object
    
    'then assigns array of the three sheet names
    Set sheetsArray = ThisWorkbook.Sheets(Array("Inv2 TS", "Inv2 Exp", "Inv2 Sum"))
    

   
    Application.ScreenUpdating = False
    ' this one first check if the name is "show"
    If Sheet6.Name = "Show Inv2" Then

' if the abovementioned condition was true,  then below code will loop only in the sheets Inv2 TS", "Inv2 Exp", "Inv2 Sum
' and set them visible"unhide)
        For Each sheet In sheetsArray
            sheet.Visible = xlSheetVisible
        Next sheet

' here it changes the name of then worksheet to Hide Inv2 and color to yellow
        Sheet6.Name = "Hide Inv2"
        Sheet6.Tab.Color = vbYellow

'activates the first sheet which is the invoice summary
        Sheet10.Activate
        
        ' now the above action will run if the sheet6 name was "Show Inv2"  but
        'if it is Not Show Inv2 then here the Else statement will run the below code
    Else

 ' this will loop through sheet array which are "Inv2 TS", "Inv2 Exp", "Inv2 Sum"
        For Each sheet In sheetsArray
        
        ' now this condition is checking if that it only should affect sheets that its name are not Sheet6 and Sheet1 and sheet10
        ' you can look there which sheets codes name are 6, 1, 10
        'then if the condition met, and exlcuded 6, 1, 10 then hide those sheets,
            If (sheet.Name <> Sheet6.Name And sheet.Name <> Sheet1.Name And sheet.Name <> Sheet10.Name) Then
               sheet.Visible = xlSheetVeryHidden
            End If
        Next sheet
' then set a new name which is show Inv2 to the sheet6 name and also change color to green.
        Sheet6.Name = "Show Inv2"
        Sheet6.Tab.Color = vbGreen

        Sheet10.Activate
    End If
    Application.ScreenUpdating = True
End Sub

 

is important to mentioned here that the code can be simplified as below.

 

since we are running the code into an array of three sheets "Inv2 TS", "Inv2 Exp", "Inv2 Sum"

 

then it is unnecessary to have this condition

If (sheet.Name <> Sheet6.Name And sheet.Name <> Sheet1.Name And sheet.Name <> Sheet10.Name) Then
End If 

so in the code below, i removed the If (sheet.Name <> Sheet6.Name And sheet.Name <> Sheet1.Name And sheet.Name <> Sheet10.Name) Then   along with it closing End If

 

 

Private Sub Worksheet_Activate()
    Dim sheet As Worksheet

    Dim sheetsArray As Sheets  ' declares sheetsarray as sheets which is sheets is an object
    
    'then assigns array of the three sheet names
    Set sheetsArray = ThisWorkbook.Sheets(Array("Inv2 TS", "Inv2 Exp", "Inv2 Sum"))
    

   
    Application.ScreenUpdating = False
    ' this one first check if the name is "show"
    If Sheet6.Name = "Show Inv2" Then

' if the abovementioned condition was true,  then below code will loop only in the sheets Inv2 TS", "Inv2 Exp", "Inv2 Sum
' and set them visible"unhide)
        For Each sheet In sheetsArray
            sheet.Visible = xlSheetVisible
        Next sheet

' here it changes the name of then worksheet to Hide Inv2 and color to yellow
        Sheet6.Name = "Hide Inv2"
        Sheet6.Tab.Color = vbYellow

'activates the first sheet which is the invoice summary
        Sheet10.Activate
        
        ' now the above action will run if the sheet6 name was "Show Inv2"  but
        'if it is Not Show Inv2 then here the Else statement will run the below code
    Else
'code will loop only in the sheets Inv2 TS", "Inv2 Exp", "Inv2 Sum
' and set them visible"unhide)
        For Each sheet In sheetsArray
                  
               sheet.Visible = xlSheetVeryHidden
        Next sheet
' then set a new name which is show Inv2 to the sheet6 name and also change color to green.
        Sheet6.Name = "Show Inv2"
        Sheet6.Tab.Color = vbGreen

        Sheet10.Activate
    End If
    Application.ScreenUpdating = True
End Sub

Related Conversations