How to Hide All Sheets in Excel Using VBA

hide-all-sheets-in-excel-using-vba-simplemsoffice

Suppose you have an Excel Workbook with multiple sheets. If you want to hide all sheets except one active sheet then you can use this code.

It is very easy to code. Just follow the below steps and you are done.

Step 1: You have to open Visual Basic Editor in Excel. Press the key Alt+F11.

Step 2: Now insert the new Module in Editor Window. Click on Insert and select Module.

Step 3: After the new module is added, just copy the below code, and you are done:

1
2
3
4
5
6
7
Sub HideAllSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name<>ActiveSheet.Name Then ws.Visible = xlSheetHidden
Next ws
Sheets("Sheet1").Range("A1").Select
End Sub

Code with Explanation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Sub HideAllSheets()

    'Declare Variable ws for Worksheet
     Dim ws As Worksheet    

    'Now apply loop. This loop will check all sheets one by one and hide them one by one. It will run until all sheets are hidden except active sheet.
     For Each ws In ActiveWorkbook.Worksheets
        If ws.Name<>ActiveSheet.Name Then ws.Visible = xlSheetHidden
     Next ws
   
    'Type the sheet name on which you want cursor active or active sheet.
     Sheets("Sheet1").Select
    'If you want to place the cursor on particular a cell value then add the below line.
     Range("A1").Select

End Sub

Step 4: After adding the code, save the file. Go to the File menu and click on Save or press Ctrl+S.

Step 5: After saving the file, return to excel and you can run your macro from the macro options. So Go to the File menu and Click on the last option Close and Return to Microsoft Excel or press Ctrl+Q.

Note:- If you want to edit VBA code, then follow Step 1 and after editing, follow Steps 4 and 5.


 

0 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *