Administrator password code not working

ndfightingirish1971

New Member
Joined
May 14, 2013
Messages
6
I am working on a data collection sheet that is going to be used by the operators on the shop floor. I need to limit the access for changing tolerances, manually changing data, modifying formulas, etc. I have a "USER" button and "ADMINISTRATOR" button that are linked to macros. The macros work in locking everything that needs to be locked for the users and unlocking everything when the administrator needs to add or modify features, however, the password I set does not function properly. It does not matter what you put into the userform. Once you click OK, it unlocks. I am completely new to writing code in userforms, so I would greatly appreciate some assistance. Thanks in advance!

Userform code:

Private Sub PASSWORD_CLICK()
If Me.Password.Value = "password" Then
LoginFlag = True
Me.Hide
Unload Me
Exit Sub
End If
End If

MsgBox "INCORRECT PASSWORD!"
End Sub


Private Sub CommandButton1_Click()
Hide
Unload Me
End Sub


Private Sub CommandButton2_Click()
Unload Me
End Sub




Private Sub TextBox1_Change()


End Sub

______________________________________________________________________________________________

Administrator code:

Sub ADMIN()
Password.Show
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "MAIN MENU" Then ws.Visible = xlSheetVisible
Next
For Each Sheet In Sheets
Sheet.Activate
ActiveWindow.DisplayGridlines = True
ActiveWindow.DisplayHeadings = True
Next Sheet
Sheets("MAIN MENU").Select
ActiveSheet.Unprotect Password:="password"
Sheets("LOG SHEET").Select
ActiveSheet.Unprotect Password:="password"
Sheets("PARAMETERS SHEET").Select
ActiveSheet.Unprotect Password:="password"
Sheets("PART MEASUREMENT").Select
ActiveSheet.Unprotect Password:="password"
Sheets("CAPABILITY SHEET").Select
ActiveSheet.Unprotect Password:="password"
Sheets("DATA STORAGE").Select
ActiveSheet.Unprotect Password:="password"
Sheets("MAIN MENU").Select
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi,
you have hard coded the password in your code so it will unprotect regardless of what you enter in your forms textbox

Perhaps update to your code may do what you want


Code:
Sub ADMIN()
    Dim ws As Worksheet
    Dim arr As Variant, m As Variant
    Const wsPassword As String = "password"
    
    arr = Array("LOG SHEET", "PARAMETERS SHEET", "CAPABILITY SHEET", "DATA STORAGE")
    
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "MAIN MENU" Then
            ws.Visible = xlSheetVisible
            m = Application.Match(ws.Name, arr, 0)
                If Not IsError(m) And Me.Password.Value = wsPassword Then
                    ws.Unprotect Password:=wsPassword
                Else
                    ws.Protect Password:=wsPassword
                End If
        End If
    Next ws
    
End Sub

Solution untested but should give you something to work with

Dave
 
Upvote 0
Thanks Dave! I tested it out and it gives me a compile error (invalid use of Me keyword) but I will see if I can work with it from there. I completely understand formulas and some easy VBA code but passwords is a completely new frontier for me. Thanks again!!!
 
Upvote 0
you get that because I assumed code would be in your userforms code page.

You can use code in standard module but you will need to pass an instance of your userform to it as an argument to reference the control.

Dave
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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