+ Reply to Thread
Results 1 to 2 of 2

Vba to password protect sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2013
    Location
    London, England
    MS-Off Ver
    Excel 365
    Posts
    6

    Vba to password protect sheet

    Hi all,

    Hope someone can help me. I've got a workbook and I need one sheet password protected (even from read).
    I've managed to more or less do what I want with the following code (Note: This needs to work at a basic level not against a determined hacker!).

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim MySheets As String, Response As String
    MySheet = "Headcount"
    If ActiveSheet.Name = MySheet Then
    ActiveSheet.Visible = False
        Response = InputBox("Enter password to view sheet")
            If Response = "password" Then
                Sheets(MySheet).Visible = True
                Application.EnableEvents = False
                Sheets(MySheet).Select
                Application.EnableEvents = True
            End If
    End If
    End Sub

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
            Cancel As Boolean)
            MySheet1 = "Sheet1"
            MySheet2 = "Headcount"
            Worksheets(MySheet1).Visible = True
            Worksheets(MySheet2).Visible = xlVeryHidden
            End Sub
    Basically this means that whenever someone click on the sheet it prompts for a password, and if anyone saves the file whilst the said sheet is active- then said sheet is 'veryhidden' immediately before save (this means that the next user can't see the sheet when they open the file, and also that they can't access the sheet if they disable macros, as there is no way to get the sheet to reappear without macro use.
    (I've added a button to unhide the sheet if it is in veryhidden status, and you then need to enter the relevant password).

    So all is good part from one thing. If I'm in another sheet and I start writing a formula like =SUM(....) and click on the passworded sheet whilst writing this formula then it shows me the full contents without any password prompting (which kind of defeats the purpose). I get why ... because the sheet is not activated whilst you are writing a formula - but I'm looking for a workaround this issue.

    Many thanks in advance!
    Last edited by alansidman; 11-07-2016 at 03:48 PM. Reason: code tags added

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,974

    Re: Vba to password protect sheet

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ 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. protect each sheet with different password
    By boon-yao.tek in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-08-2015, 01:39 AM
  2. [SOLVED] Protect a sheet with a password, but secure the password
    By JimDandy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2013, 01:59 AM
  3. Protect sheet with 2 or more password
    By iazis in forum Excel General
    Replies: 3
    Last Post: 05-02-2012, 07:07 AM
  4. VBA and password protect sheet.
    By esbjornsson in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-29-2012, 03:27 AM
  5. [SOLVED] Protect all with password, except 1 sheet?
    By Lewej23 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-28-2012, 12:07 PM
  6. Protect Sheet & Password Protect Code
    By KLahvic in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-13-2009, 04:08 PM
  7. Password protect 1 sheet
    By LarryC in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-03-2008, 11:08 AM

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