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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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,215,093
Messages
6,123,069
Members
449,090
Latest member
fragment

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