Prompt for Passsword if not on approved list using windows authentication


Board Regular
Aug 19, 2006
I have been searching for this without any luck.

I am looking for a code that will look at a very hidden sheet called "Approved Users" for a list of approved names that = their windows authentication (or login user name). If it is found then the password prompt is bypassed and they are allowed into the workbook. If not found then the user has to know the password to have access to the workbook.

Not sure if this is even possible????

Thank you


Board Regular
Aug 19, 2006
Re: Prompt for Passsword if not on approved list using windows authentication - NOT SOLVED

Ok I have this code and it works except I can not get to actually insert the password and open the work book. Or just bypass the prompt if the user name is on the list.

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Workbook_Open()
Dim UName As String * 255
Dim L As Long: L = 255
Dim Res As Long
    Res = GetUserName(UName, L)
    On Error GoTo notfound
    If Range("A1:A10").Find(What:=UName, MatchCase:=False, LookAt:=xlWhole).Select Then 'Change false to true if you want case sensetive
        Call FoundIt
        Exit Sub
    End If
    If Err.Number = 91 Then 'This is done to allow other error messages to still report correctly
        MsgBox "Not Found"
        MsgBox "Error: " & Err.Number & " - " & Err.Description
    End If
End Sub
Sub FoundIt()

Dim strPassword As String
strPassword = "1"
 'protect/unprotect workbook

ActiveWorkbook.Unprotect strPassword

End Sub
Any ideas???

Jaafar Tribak

Well-known Member
Dec 5, 2002
Office Version
The workbook_open event code won't run until the password is entered and the workbook is actually opened ..

One thing you can do is have the hidden sheet and workbook_open code in an intermediary unprotected workbook .. If the user name is on the list then open the actual workbook from it and self-close the intermediary workbook
Last edited:

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...