password for each user to enter workbook

tomtat

New Member
Joined
Mar 25, 2014
Messages
10
i have some code for opening the workbook that only allows it to be opened if the correct name is entered.

would i then be able to have an individual password for each user after entering their name. if entry is wrong i would like the workbook to close.

the code i have is below


x = InputBox("Enter Your Name", "Authentication")If x = "False" Or x = "" Then MsgBox "Incorrect: you must enter a name" ActiveWorkbook.Close FalseEnd IfIf x <> "John" And x <> "Glen" Then MsgBox "Incorrect name" ActiveWorkbook.Close FalseEnd IfSheets("sheet1").Range("E3").Value = x</pre>
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi,
Add a worksheet to your workbook & Name it "User List" then enter Users name in Column A and their password in Column B. When done, you can hide it.

Use this updated version of your code & see if does what you want:

Code:
Sub Autorization()
    Dim ValidUser As Range
    Dim x(2) As Variant, sPrompt(2) As Variant
    
    i = 0
    Do
        sPrompt(i) = IIf(i = 0, "Enter Your Name", "Enter Your Password")
        x(i) = InputBox(sPrompt(i), "Authentication")
        'cancel pressed
        If StrPtr(x(i)) = 0 Then
            'cancel pressed
            ThisWorkbook.Close False
        ElseIf Len(x(i)) = 0 Then
            MsgBox "Incorrect: you must enter a name", 48, "Entry Required"
        Else
            Set ValidUser = Sheets("User List").Columns(i + 1).Find(x(i), LookIn:=xlValues, lookat:=xlWhole)
            If Not ValidUser Is Nothing Then
                If i = 1 Then
                    'user and password valid
                    Exit Do
                Else
                    i = i + 1
                End If
            Else
                msg = IIf(i = 0, x(0) & " does not have access to this file", "Invalid Password")
                MsgBox msg, 48, "Invalid Entry"
                ThisWorkbook.Close False
            End If
        End If
    Loop
End Sub

Dave.
 
Upvote 0
Why not try and post back with specific questions.

If you open your workbook and the example by selecting the userform in the properties Window of the VB EDitor you can actually drag & drop it to your workbook.
 
Upvote 0
thanks for your response dave, this works how i want it to work. the only problem is i need it to put the name of the user in sheet1- cell E3. this is because i have a sheet that records who opens the workbook. from the name that is put in this cell i have some other code that puts it in a list on a table.

thanks
Tom
 
Upvote 0
thanks for your response dave, this works how i want it to work. the only problem is i need it to put the name of the user in sheet1- cell E3. this is because i have a sheet that records who opens the workbook. from the name that is put in this cell i have some other code that puts it in a list on a table.

thanks
Tom

Hi Tom,
just add a line of code to do that after the word Loop.

Code:
Sheets("Sheet1").Range("E3").Value = x(0)

Dave
 
Upvote 0

Forum statistics

Threads
1,215,758
Messages
6,126,713
Members
449,332
Latest member
nokoloina

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top