Password Input prompt to Protect/Unprotect entire workbook and all worksheets

rach_oh

New Member
Joined
Aug 2, 2023
Messages
19
Office Version
  1. 2016
Platform
  1. Windows
What is the best way to go about adding an input box to the below code that would prompt for a password, notify if password is wrong, and close input box without completing if incorrect?

Private Sub CommandButton6_Click()
Workbooks("Budget Template - Working 8.7").Protect "test123"
Dim ws As Worksheet
For Each ws In Worksheets
ws.Protect "password"
Next ws
End Sub

Private Sub CommandButton7_Click()
Workbooks("Budget Template - Working 8.7").Unprotect "test123"
Dim ws As Worksheet
For Each ws In Worksheets
ws.Unprotect "password"
Next ws
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
This is the updated code - Input box is allowing another password beside "test123" to be used to lock/unlock. How do I work around this?

Private Sub CommandButton6_Click()
Workbooks("Budget Template - Working 8.7").Protect "test123"
Dim ws As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter password to protect all sheets", "test123")
On Error Resume Next
For Each ws In Worksheets
ws.Protect Password:=Pwd
Next ws
If Err <> 0 Then
MsgBox "Incorrect Password"
End If
On Error GoTo 0
End Sub

Private Sub CommandButton7_Click()
Workbooks("Budget Template - Working 8.7").Unprotect "test123"
Dim ws As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter password to unprotect all sheets", "test123")
On Error Resume Next
For Each ws In Worksheets
ws.Unprotect Password:=Pwd
Next ws
If Err <> 0 Then
MsgBox "Incorrect Password"
End If
On Error GoTo 0
End Sub
 
Upvote 0
Edit to meet your needs. If you want a specific password to be used don't use InputBox on Protect.
VBA Code:
Private Sub CommandButton1_Click()
' Protect Workbook with this button
Dim pWord As String
pWord = InputBox("Enter password", "Lock")
ThisWorkbook.Protect (pWord)
End Sub

Private Sub CommandButton2_Click()
'Unprotect Workbook with this button
Dim pWord As String
pWord = InputBox("Enter password", "Unlock")
On Error GoTo wrongPword
ThisWorkbook.Unprotect (pWord)
Exit Sub
wrongPword:
    MsgBox "Password is incorrect", vbCritical, "Try Again"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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