Incorrect Password still runs macro

PaulWJ

New Member
Joined
Dec 4, 2023
Messages
12
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm trying to run a macro that unlocks certain sheets, but I want a password to do so (for security). I got a solution from the microsoft community site, but I can enter the wrong password, or just hit the cancel button and the macro still runs. Double-checked the post, but I've followed the right steps. Can anyone help please?

I've added the MS site, and confirmation I've done the first part as images. The code I've got is:

Sub UnlockSheet()

' Unlocks all the sheets and adds the tabs, column and row headers back in

Dim password As Variant
password = Application.InputBox("Enter Maintenance Password", "Password Protected")


ActiveWindow.DisplayWorkbookTabs = True
ActiveWindow.DisplayHeadings = True
Sheets("Intro").Unprotect password:="relentless"
Sheets("ICC").Unprotect password:="relentless"
Sheets("GeneralQuery").Unprotect password:="relentless"
Sheets("ResCall").Unprotect password:="relentless"
Sheets("Background").Visible = xlSheetVisible
Sheets("Background").Select



End Sub

MS Site.PNGVBA Protection.PNG
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Nothing in that code does anything at all with the password that is input. It should have something like:

VBA Code:
Sub UnlockSheet()

' Unlocks all the sheets and adds the tabs, column and row headers back in

Dim thepassword As Variant
thepassword = Application.InputBox("Enter Maintenance Password", "Password Protected")
if thepassword <> "relentless" then 
msgbox "Incorrect password"
exit sub
end if

ActiveWindow.DisplayWorkbookTabs = True
ActiveWindow.DisplayHeadings = True
Sheets("Intro").Unprotect password:=thepassword
Sheets("ICC").Unprotect password:=thepassword
Sheets("GeneralQuery").Unprotect password:=thepassword
Sheets("ResCall").Unprotect password:=thepassword
Sheets("Background").Visible = xlSheetVisible
Sheets("Background").Select



End Sub
 
Upvote 0
You have gone to the trouble of asking for a password, but then your code ignores that and uses the real password. Below is your code, amended slightly, which should do what you want.
VBA Code:
Sub UnlockSheet()
    ' Unlocks all the sheets and adds the tabs, column and row headers back in
    Dim strPassword As String
    '
    strPassword = Application.InputBox("Enter Maintenance Password", "Password Protected")
    If (Trim(strPassword) <> vbNullString) Then
        On Error GoTo Err_Exit
        Sheets("Intro").Unprotect strPassword
        Sheets("ICC").Unprotect strPassword
        Sheets("GeneralQuery").Unprotect strPassword
        Sheets("ResCall").Unprotect strPassword
        Sheets("Background").Visible = xlSheetVisible
        Sheets("Background").Select
        ' Put these commands here, so that they only get executed if the password is okay.
        ActiveWindow.DisplayWorkbookTabs = True
        ActiveWindow.DisplayHeadings = True
    End If
    Exit Sub
Err_Exit:
    Err.Clear
    MsgBox "Invalid password", vbCritical + vbOKOnly, "Unlock Sheet"
End Sub
 
Upvote 0
Nothing in that code does anything at all with the password that is input. It should have something like:

VBA Code:
Sub UnlockSheet()

' Unlocks all the sheets and adds the tabs, column and row headers back in

Dim thepassword As Variant
thepassword = Application.InputBox("Enter Maintenance Password", "Password Protected")
if thepassword <> "relentless" then
msgbox "Incorrect password"
exit sub
end if

ActiveWindow.DisplayWorkbookTabs = True
ActiveWindow.DisplayHeadings = True
Sheets("Intro").Unprotect password:=thepassword
Sheets("ICC").Unprotect password:=thepassword
Sheets("GeneralQuery").Unprotect password:=thepassword
Sheets("ResCall").Unprotect password:=thepassword
Sheets("Background").Visible = xlSheetVisible
Sheets("Background").Select



End Sub
Thanks Rory.

Think I've maybe not explained myself properly. The password the first bit of code was looking for was a 'master' password (which was different to the one in the remainder of the code).

But, the fix works, so many thanks :)
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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