Monday, January 28, 2019

How to Hide All Sheets in Excel Using VBA - Step by Step Guide


Suppose you have an Excel Workbook with multiple sheets. And you want to hide all sheets except one active sheet then this code you can use.
It is very easy code. Just follow below steps and you are done.


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

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

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


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

Sub HideAllSheets()

    'Declare Variable ws for WorksheetDim ws As Worksheet
    

    'Now apply loop. This loop will check all sheets one by one and hide them one by one. It will run untill 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 cursor on particular cell value then add below line.
    Range("A1").Select


End Sub


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

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

How to Assign Macro to a Clickable Button?


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



***** End *****

No comments:

Post a Comment