How to Hide Multiple Sheets using VBA in Microsoft Excel 2010

In this article, you will learn how to hide multiple sheets using VBA code.

Let us understand with an example:

Q) I have around 100 sheets in my Excel workbook. Only sheet1 should be visible to user and rest should be invisible. I don’t want user to unhide sheets by right clicking and unhide them.

Click on Developer tab

From Code group, select Visual Basic
 
img1
 
Click on Insert, and then Module
 
img2
 

This will create new module.

Enter the following code in the Module

Sub HideSheets()

  Dim sh As Worksheet

  For Eachsh In ThisWorkbook.Worksheets

    If sh.Name<> “Sheet1″ Then

sh.Visible = xlSheetVeryHidden

    End If

  Next

End Sub
 
img3
 
The above code will check name of each worksheet & if the name is found other than “Sheet1” then it will run & hide the sheet.

After executing the macro; we will get Sheet1 as visible to us & rest will be hidden.

To be able to view sheet1 & sheet2 only out of 100 sheets, you need the following code

Sub HideSheets2()

  Dim sh As Worksheet

  For Eachsh In ThisWorkbook.Worksheets

    If sh.Name<> “Sheet1″ Then

sh.Visible = xlSheetVeryHidden

    End If

    Else

    If sh.Name<> “Sheet2″ Then

sh.Visible = xlSheetVeryHidden

    End If

  Next

End Sub

 

HideSheet2 macro will not hide “Sheet1” & “Sheet2” & ensure all other sheets will get hidden.

In this way, we can hide the sheets which we do not want user to view.

 

 

Users are saying about us...

  1. How can I check Visible multiples sheets name? If not sheets visible = true

    Sub HideSheets()
    Dim sh As Worksheet
    For Each sh.Name In ThisWorkbook.Worksheets(array(“Com1″,”com3″,”com4″)
    If sh.Visible = xlSheetvisible then
    ‘ Do nothing
    else
    sh.Visible = xlsheetvisible
    End If
    Next

    End Sub

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Visit Us On TwitterVisit Us On FacebookVisit Us On Google PlusVisit Us On Youtube