+ Reply to Thread
Results 1 to 3 of 3

Hiding sheets using VBA and a password

  1. #1
    Registered User
    Join Date
    07-09-2014
    Location
    Edmonton Alberta
    MS-Off Ver
    2010
    Posts
    2

    Hiding sheets using VBA and a password

    Hi,

    I'm trying to create a button that will run two macros:
    a) "HideSheets": toggle the visibility of Sheet2 between visible and very hidden
    b) "Auto_Close": Runs automatically when the user attempts to close the book. It hides the sheets set in that macro; in this case it’s set to hide Sheet2. The user is then prompted to save the book. The purpose of this is to hide the sheets in case the user forgets to hide them before closing.

    I can't get it to work the way I want - I get the password prompt but nothing ever toggles between visible/veryhidden.

    Any assistance is appreciated.

    Thanks!


    The Code:
    Sub HideSheets()

    ' This macro toggles worksheets listed between visible and very hidden
    ' and requires a password to run
    Dim myPassword As String

    myPassword = "test" ' Set password here

    Password = InputBox("Enter Password")
    If Password = "" Then Exit Sub ' Exit if null input or cancel

    ' Incorrect password
    If Password <> myPassword Then
    MsgBox Title:="Warning", prompt:="Incorrect Password"
    Exit Sub
    End If

    On Error Resume Next

    ' Correct password
    If Worksheets("Sheet2").Visible = True Then ' Check if one is visable.
    ' The sheet above is visiable so hide all required
    Worksheets("Sheet2").Visible = xlSheetVeryHidden

    Else
    ' Sheets must be hidden, so unhide all required
    Worksheets("Sheet2").Visible = True

    End If

    End Sub

    Private Sub Auto_Close()
    ' This macro hides sheets when the user closes the workbook
    ' It is a "Private Sub" to prevent it being listed with outher macros

    On Error Resume Next

    ' When workbook is closed hide sheets required
    Worksheets("Sheet2").Visible = xlSheetVeryHidden

    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Hiding sheets using VBA and a password

    Remove the On Error line. It shouldn't be needed and it could be masking the actual problem. Otherwise, the code worked for me.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    07-09-2014
    Location
    Edmonton Alberta
    MS-Off Ver
    2010
    Posts
    2

    Re: Hiding sheets using VBA and a password

    Thanks for the reply AlphaFrog.

    All is working now! so random...

    Thanks again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hiding Sheets based on cell value, only hiding last sheet
    By mattblack2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2014, 03:21 PM
  2. [SOLVED] Hiding an Excel Sheet with Password
    By V.G.PADMANABHAN in forum Excel General
    Replies: 3
    Last Post: 10-31-2012, 02:23 AM
  3. Hiding Formulas by password
    By Bikash Shaw in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-21-2012, 05:29 AM
  4. Hiding a Worksheet w/a Password?
    By RED Rose in forum Excel General
    Replies: 6
    Last Post: 09-02-2010, 08:11 PM
  5. Hiding cells with password
    By cardingtr in forum Excel General
    Replies: 2
    Last Post: 08-09-2006, 10:46 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1