+ Reply to Thread
Results 1 to 6 of 6

Username and Password VBA with the ability for user to change the password

Hybrid View

  1. #1
    Registered User
    Join Date
    01-07-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    65

    Username and Password VBA with the ability for user to change the password

    Hey Guys,

    I am just working on a project and it requires that multiple people to login to one workbook with various access.

    I use the code I got from below;

    http://www.mrexcel.com/forum/excel-q...xcel-file.html

    Private Sub Workbook_Open()
    Dim Sh As Worksheet
    Dim UserName As String
    Dim Password As String
    Dim ThisCell As Range
    Dim c As Long
        For Each Sh In ThisWorkbook.Worksheets
            If Sh.Name <> "Welcome" Then
                Sh.Visible = xlSheetVeryHidden
            End If
        Next Sh
        UserName = InputBox("Please enter your user name.")
        Password = InputBox("Please enter password.")
        For Each ThisCell In Sheets("User List").Range("A2:A" & Sheets("User List").Range("A65536").End(xlUp).Row)
            If UCase(ThisCell.Value) = UCase(UserName) And UCase(ThisCell.Offset(, 1).Value) = UCase(Password) Then
                MsgBox "Access Granted"
                For c = 2 To 4
    'This is the number of sheets from C1 to E1
                    If ThisCell.Offset(, c).Value <> "" Then
                        Sheets(Sheets("User List").Cells(1, c + 1).Value).Visible = xlSheetVisible
                    End If
                Next c
                Exit Sub
            End If
        Next ThisCell
        MsgBox "Access Denied"
        ThisWorkbook.Close
    End Sub

    And this works perfectly fine but I need user to be able to amend his/her password after a successful login.

    And if the logging fails I like the code to ask whether user want to close the workbook or want to try entering username and password again.

    Also if user forgets his password it would be great if the code can send an email to user containing the password.

    Finally I like to keep a log for each successful logins... Date, time and user name.Thanks for you help in advance.
    Last edited by zbor; 04-24-2014 at 05:02 AM.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Username and Password VBA with the ability for user to change the password

    Hi, egemencoskun,

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    http://www.ozgrid.com/forum/showthread.php?t=187396

    And you should wrap the procedure with code-tags for better readability.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,053

    Re: Username and Password VBA with the ability for user to change the password

    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 here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Never use Merged Cells in Excel

  4. #4
    Registered User
    Join Date
    01-07-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    65

    Re: Username and Password VBA with the ability for user to change the password

    Thanks that was very helpful...

  5. #5
    Registered User
    Join Date
    01-07-2013
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    65

    Re: Username and Password VBA with the ability for user to change the password

    Mike at Ozgrid kindly provided me with this code and I thought I should share it here to help others... I hoe you would find this helpful and it works great for me.

    Linkt to Mikes reply is here - http://www.ozgrid.com/forum/showthread.php?t=187396

    Also the spreadsheeet is attached

     Sub Workbook_Open() 
        If IsOKUser Then 
            If ThisWorkbook.Sheets("UserData").Visible = xlSheetVisible Then 
                MsgBox "you are an administrator" 
            Else 
                MsgBox "you are an authorized user" 
            End If 
        Else 
            MsgBox "You are not authorized to use this workbook" 
            ThisWorkbook.Close 
        End If 
         
    End Sub 
     
    Function IsOKUser() As Boolean 
        Dim uiUserName As String 
        Dim uiPW As String, uiVerifyPW As String 
        Dim rngUserNames As Range, rngUserFound As Range 
        Dim rngHistory As Range 
        Dim strPrompt As String 
        Dim i As Long, oneSheet As Worksheet 
         
        Dim maxTry As Long: maxTry = 2 
         
        With ThisWorkbook.Worksheets("UserData") 
            Set rngUserNames = .Columns(1) 
            Set rngHistory = .Columns("H:H") 
        End With 
         
        Rem hide sheets 
        Application.ScreenUpdating = False 
        With ThisWorkbook 
            With .Sheets("Welcome") 
                .Visible = xlSheetVisible 
                .Activate 
            End With 
            For Each oneSheet In .Sheets 
                If oneSheet.Name <> ActiveSheet.Name Then 
                    oneSheet.Visible = xlSheetVeryHidden 
                End If 
            Next oneSheet 
        End With 
        Application.ScreenUpdating = True 
        Application.ScreenUpdating = False 
         
        Rem verify password 
        Do While (0 < maxTry) 
            IsOKUser = False 
            maxTry = maxTry - 1 
             
            uiUserName = Application.InputBox("Enter case insensitive UserName", Type:=2) 
            If uiUserName = "False" Then Exit Function: Rem cancel pressed 
            With rngUserNames.EntireColumn 
                Set rngUserFound = .Find(uiUserName, after:=.Cells(1, 1), MatchCase:=False, lookat:=xlWhole) 
            End With 
            If rngUserFound Is Nothing Then 
                Rem no matching username 
                MsgBox Application.Proper(uiUserName) & " is not an authorized username" 
            Else 
                uiPW = Application.InputBox("Enter case sensitive password", Type:=2) 
                If uiPW = "False" Then Exit Function: Rem canceled 
                IsOKUser = (uiPW = CStr(rngUserFound.Offset(0, 1).Value)) 
                 'IsOKUser = (LCase(uiPW) = LCase(CStr(rngUserFound.Offset(0, 1).Value))): Rem case insensitive passwords
            End If 
             
            If IsOKUser Then 
                Rem user/password match 
                Application.ScreenUpdating = False 
                maxTry = 0 
                 
                Rem reveal user 's sheets
                For i = 2 To 4 
                    With rngUserFound.Offset(0, i) 
                        If .Value <> vbNullString Then 
                            ThisWorkbook.Sheets(.EntireColumn.Cells(1, 1).Value).Visible = xlSheetVisible 
                        End If 
                    End With 
                Next i 
                 
                Rem test If admin 
                If rngUserFound.Offset(0, 5).Value <> vbNullString Then 
                    For Each oneSheet In ThisWorkbook.Sheets 
                        oneSheet.Visible = xlSheetVisible 
                    Next oneSheet 
                End If 
                 
                Rem record log-in 
                With rngHistory 
                    With .Cells(.Rows.Count).End(xlUp).Offset(1, 0) 
                        .Value = rngUserFound.Value 
                        .Offset(0, 1).Value = Now 
                    End With 
                End With 
                Application.ScreenUpdating = True 
                 
                Rem New password 
                Do 
                    strPrompt = "Enter a new password" & vbCr & "or press cancel or Return to not change pw." 
                    uiPW = Application.InputBox(strPrompt, Default:=rngUserFound.Offset(0, 1).Value, Type:=2) 
                    If uiPW = "False" Or uiPW = vbNullString Then Exit Function: Rem canceled 
                     
                    If uiPW = rngUserFound.Offset(0, 1).Value Then 
                        Rem New password = old password 
                        uiVerifyPW = uiPW 
                    Else 
                        strPrompt = "Enter your new password again" 
                        uiVerifyPW = Application.InputBox(strPrompt, Default:=vbNullString, Type:=2) 
                         
                        If uiVerifyPW = uiPW Then 
                            Rem entries match, password changed 
                            MsgBox "Password changed" 
                            rngUserFound.Offset(0, 1) = uiPW 
                        Else 
                            Rem New password verification mismatch 
                            MsgBox "Password verification entry failed. Try again." 
                        End If 
                    End If 
                Loop Until uiPW = uiVerifyPW 
            Else 
                MsgBox "Authorization Failed. You have " & maxTry & " attempts left." 
            End If 
        Loop 
    End Function 
     
    Private Sub Workbook_BeforeClose(Cancel As Boolean) 
        Dim oneSheet As Worksheet, wasSaved As Boolean 
        Application.ScreenUpdating = False 
         
        With ThisWorkbook 
            wasSaved = .Saved 
             
            With .Sheets("Welcome") 
                .Visible = xlSheetVisible 
                .Activate 
            End With 
            For Each oneSheet In .Sheets 
                If oneSheet.Name <> ActiveSheet.Name Then 
                    oneSheet.Visible = xlSheetVeryHidden 
                End If 
            Next oneSheet 
             
            If wasSaved Then 
                .Save 
            Else 
                If MsgBox("Save Changes", vbYesNo) = xlYes Then 
                    .Save 
                Else 
                    .Saved = True 
                End If 
            End If 
        End With 
        Application.ScreenUpdating = True 
    End Sub
    Thanks to Mike :-)
    Attached Files Attached Files

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,053

    Re: Username and Password VBA with the ability for user to change the password

    Thank you for sharing code.
    However, you should follow the rules.
    I will edit your post and add code tags.

    About cross-posting, I hope you see that if your question is answered somewhere else then solvers unnecessarily spend their time. That's why providing link to other forums is needed.

+ 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. Replies: 6
    Last Post: 02-09-2013, 03:06 AM
  2. [SOLVED] Username and Password Form - Show Username in Sheet
    By ryan180 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2013, 07:06 PM
  3. user input of password for username
    By tinkerbelle in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-08-2012, 03:03 AM
  4. Display specific user's sheet when username and password is correct
    By stoey in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2012, 04:40 AM
  5. User Password On A Button - Encrypt Password
    By mab in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-05-2010, 08:10 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