+ Reply to Thread
Results 1 to 15 of 15

Require password input from all UNLESS the user is a specific name

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    890

    Question Require password input from all UNLESS the user is a specific name

    Afternoon Happy Campers,

    I have the following code on a workbook.
    Private Sub CommandButton2_Click()
        Application.ScreenUpdating = False
    
        Dim wSheet          As Worksheet
        Dim Pwd             As String
         
        Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
        On Error Resume Next
        For Each wSheet In Worksheets
            wSheet.Unprotect Password:=Pwd
        Next wSheet
        If Err <> 0 Then
            MsgBox "For access please contact  " & _
            "This Person", vbCritical, "Please Try Again"
        End If
        On Error GoTo 0
        Application.ScreenUpdating = True
        ThisWorkbook.Save
    
    End Sub
    Which runs very nicely indeed. any user who opens the sheet must supply the correct password, or the sheet does not unlock.
    But i would like to know if i can get VBA to look at the windows login name of the person trying to unlock, and if it matches my own (Bloggs, Joe) then when i click the button for unlocking the sheet, VBA will bypass the need to enter the password? but for any other username it will require them to enter a password?
    Not entirely sure if its too much of a complicate thing (doubtful) or not, but im guessing im going to get the best answer from this forum!!
    (The sheet is my own creation and i also have another button with a predefined password)
    At present the sheet works, very well (with lots of input from here lol) i would just like to put the finishing touches onto it, as a kind of bragging right lol

    kind regards, galvinpaddy.

  2. #2
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    890

    Re: Require password input from all UNLESS the user is a specific name

    and i have just noticed how much i appear to like the 'LOL' abbreviation.

    lol

  3. #3
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Require password input from all UNLESS the user is a specific name

    Try the code below on a copy of your workbook as untested

    Private Sub CommandButton2_Click()
        Application.ScreenUpdating = False
    
        Dim wSheet          As Worksheet
        Dim Pwd             As String
    
        If Environ("USERNAME") = "Bloggs, Joe" Then
            For Each wSheet In Worksheets
                wSheet.Unprotect
            Next
        Else
            Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
            On Error Resume Next
            For Each wSheet In Worksheets
                wSheet.Unprotect Password:=Pwd
            Next wSheet
            If Err <> 0 Then
                MsgBox "For access please contact  " & _
                       "This Person", vbCritical, "Please Try Again"
            End If
            On Error GoTo 0
            Application.ScreenUpdating = True
            ThisWorkbook.Save
        End If
    End Sub
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  4. #4
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    890

    Re: Require password input from all UNLESS the user is a specific name

    Hi WasWodge,
    Thanks for the effort, however it does not appear to work.

  5. #5
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Require password input from all UNLESS the user is a specific name

    That is a bit strange as I tried it later in the day on my laptop and it worked for me. Did you type in your username exactly as it is written? I.e. No different spaces. The username is the name you sign on to your pc with and it must be exactly the same as the password. On my phone until this evening and so I won't be able to test anything until then.

  6. #6
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    890

    Re: Require password input from all UNLESS the user is a specific name

    Ah, ok.
    In excel VBA i have a function - Application.UserName
    This shows my name as Surname, Firstname - however - logging into windows itself my login is Firstname.Surname
    thanks for highlighting that, i will give it a try shortly.
    Regards.

  7. #7
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Require password input from all UNLESS the user is a specific name

    MsgBox "Current user is " & Environ("UserName")

  8. #8
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    890

    Re: Require password input from all UNLESS the user is a specific name

    hi, thanks that has shown me my username as it should be. but still doesnt unlock all sheets.
    i have the following codes.
    Private Sub CommandButton1_Click()
         
        Dim wSheet          As Worksheet
        Dim Pwd             As String
         
        Pwd = "RichG"
        For Each wSheet In Worksheets
            wSheet.Protect Password:=Pwd
        Next wSheet
        ThisWorkbook.Save
        
    End Sub
    and
    Private Sub CommandButton2_Click()
        Application.ScreenUpdating = False
    
        Dim wSheet          As Worksheet
        Dim Pwd             As String
    
        If Environ("USERNAME") = "richard.galvin" Then
            For Each wSheet In Worksheets
                wSheet.Unprotect
            Next
        Else
            Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
            On Error Resume Next
            For Each wSheet In Worksheets
                wSheet.Unprotect Password:=Pwd
            Next wSheet
            If Err <> 0 Then
                MsgBox "For access please contact  " & _
                       "richard.galvin", vbCritical, "Please Try Again"
            End If
            On Error GoTo 0
            Application.ScreenUpdating = True
            ThisWorkbook.Save
        End If
    End Sub
    Should the codes unlock the sheet for me, without having to click the command button?
    EDIT * the code is also asking me to enter the password for all of the sheets one by one.

  9. #9
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Require password input from all UNLESS the user is a specific name

    It sounds like you need the code for your username in a workbook open event . I will have a look this evening when I get in. As for the sheets needing to have the password entered for each sheet I didn't alter your original code but again I will have a look when I get in

  10. #10
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Require password input from all UNLESS the user is a specific name

    Hi Richard, Try placing the code below in the ThisWorkbook module. Leave your original code in the standard module.

    To place code in the ThisWorkbook module

    Copy the Excel VBA code that you want to use
    Select the workbook in which you want to store the code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    In the Project Explorer, find your workbook, and open the list of Microsoft Excel Objects
    Right-click on the ThisWorkbook object, and choose View Code
    Where the cursor is flashing, choose Edit | Paste

    Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Dim myPassword As String
        Dim wSheet As Worksheet
    myPassword = "Pwd"
        If Environ("USERNAME") = "richard.galvin" Then
            For Each wSheet In ActiveWorkbook.Worksheets
             wSheet.Unprotect Password:=myPassword
            Next
            End If
    Application.ScreenUpdating = True
    End Sub
    Last edited by WasWodge; 07-02-2012 at 11:01 AM.

  11. #11
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    890

    Re: Require password input from all UNLESS the user is a specific name

    hahahaha, fantastic, thanks very much indeed
    didnt occur to me that it would need to be in 'ThisWorkbook'
    Sincere thanks!!!

    regards, rich

  12. #12
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    890

    Re: Require password input from all UNLESS the user is a specific name

    sorry, few final questions to this if i may, how do i add more than one username to this code, and is it possible to have a text box/user form/ etc pop up, so when it sees the name that allows the sheets to be unlocked, pops up and asks - "Unlock All?" clicking yes unlocks, clicking no leaves locked?

    kind regards.
    Last edited by galvinpaddy; 07-03-2012 at 01:30 AM.

  13. #13
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Require password input from all UNLESS the user is a specific name

    I will see if I can get a chance to have a look this evening if I get a chance when I am on my pc rather than my phone

  14. #14
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Require password input from all UNLESS the user is a specific name

    Hi Rich, try putting this in the ThisWorkbook module
    Private Sub Workbook_Open()
        Dim objNet As Object
        Dim Msg As String
        Set objNet = CreateObject("WScript.NetWork")
        Select Case objNet.UserName
        Case Is = "Clve", "richard.galvin", "John", "Mary"
            Call Rich2
        Case Else
            Call Rich1
        End Select
    End Sub
    And put the code below in a standard module
    Sub Rich1()
        Dim wSheet As Worksheet
        Dim Pwd As String
        Application.ScreenUpdating = False
        Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
        On Error Resume Next
        For Each wSheet In Worksheets
            wSheet.Unprotect Password:=Pwd
        Next wSheet
        If Err <> 0 Then
            MsgBox "For access please contact  " & _
                   "This Person", vbCritical, "Please Try Again"
        End If
        On Error GoTo 0
        Application.ScreenUpdating = True
        ThisWorkbook.Save
    End Sub
    Sub Rich2()
    Dim myPassword As String
    Dim wSheet As Worksheet
    Dim Rly As Integer
    Application.ScreenUpdating = False
        Rly = MsgBox("You have been granted access. To unlock all sheets press Yes", 68)
      If Rly = 6 Then
    myPassword = "Pwd"
    On Error Resume Next
    For Each wSheet In ActiveWorkbook.Worksheets
             wSheet.Unprotect Password:=myPassword
            Next
            Else
           Exit Sub
           End If
    Application.ScreenUpdating = True
    End Sub
    Please note: You should really take the solved off the post if you are adding another question.
    Last edited by WasWodge; 07-03-2012 at 10:46 PM.

  15. #15
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    890

    Re: Require password input from all UNLESS the user is a specific name

    Fantastic code, thank-you very much indeed, greatly appreciated.
    Rep added.
    regards, galvinpaddy

+ 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