+ Reply to Thread
Results 1 to 3 of 3

Hide & protect sheet

  1. #1
    Registered User
    Join Date
    04-30-2009
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2003
    Posts
    23

    Hide & protect sheet

    Hello guys.

    I'm trying to protect a sheet so that it can be unhidden only if you know the password.
    This is because I have a macro that updates some of the cells and there are some people (and only those) that should be able to access it quickly and easily. -> can't use VeryHidden or cell lock.

    I have attached a small example I am currently developing.

    I have a form that pops-up when you activate the sheet in question (sheet1 in my file).
    If the password is the good one, the form disappears and sheet1 stays visible. If not, the form disappears and sheet1 is hidden again.
    I've also modified the code so that Enter has the same effect as the OK button and Escape or X button have the same effect as the CANCEL button.

    The problem is that right now if you unhide sheet1 (enter the right password), select another sheet then go to sheet1 again, the form pops-up again asking for the password.
    The great thing would be to enter the password only once and for the form to pop-up only after sheet1 has been manually hidden again.
    Is there some smart macro that could do this?

    I was thinking about catching the hide/unhide events for sheet1 if that's possible, but being a total beginner I don't know how to do that.
    Is there maybe there's a simpler way?!
    Attached Files Attached Files
    Last edited by Doctorul; 07-19-2009 at 05:57 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: Hide & protect sheet

    You need to tell you macro which state your form is in. The trouble is that because the macro is triggered when you select the sheet (either unhide it, or select it), then it is always in the same state as 'unhidden', so you can't use this property as a test. You need to create a new test for this state prior to running the macro. One way to do this is declare a static variable globally up front, then change the state in your code as you change the window state.

    Add this line outside of any existing procedure in any module

    Please Login or Register  to view this content.
    add this to the workbook sheet module

    Please Login or Register  to view this content.
    Change the code on the sheet 1 module to

    Please Login or Register  to view this content.

    After every line of code where you have hidden/unhidden the sheet, you need to add another line that updates your variable

    either MySheetHidden = True or MySheetHidden = False

    You will need to debug this to make sure it works, as I haven't tested it

  3. #3
    Registered User
    Join Date
    04-30-2009
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: Hide & protect sheet

    Thank you for the replay Mallycat!

    At first I had some trouble inserting all the right code in the right place after your suggestion.
    So...
    this goes to the "Sheet1(Sheet1)" code
    Please Login or Register  to view this content.
    this goes to the "ThisWorkbook" code
    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    in the form, wherever changes are being made to the state of Sheet1 (hidden or unhidden).

    I've spotted one small problem with this code: you were supposed to close the file to get the Boolean variable MySheetHidden to update.
    In fact, if you were hiding Sheet1 and not trying to close the file, when you would unhide it, the form wouldn't pop-up. So I had to link the variable update to one more event and because of (or thanks to) the fact that when you hide a Sheet, Excel automatically selects the next Sheet, all I had to do was to add 3 lines to the "Sheet2(Sheet2)" code:
    Please Login or Register  to view this content.
    So that's the smart macro that I was looking for and right now I personally see no flaw in it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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