Unprotect All Sheet

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
289
Office Version
  1. 2016
Platform
  1. Windows
I am currently using the code below. I want to stop the "vbCritical" error if I CANCEL the password input box. I would like it to just exit and do nothing. What am I missing?

Code:
 Sub UnProtectAll()
 
     'Use Ctl+Shift+U To Unlock All Sheets
     
    Dim ws As Worksheet
    Dim strPwd As String
    Dim strCheck As String
Application.ScreenUpdating = False
   strCheck = "mypassword"
     strPwd = InputBox("Enter Password", "Password", "Enter Password")
If strPwd = strCheck Then
  For Each ws In ThisWorkbook.Worksheets
     ws.Unprotect Password:=strPwd
  Next ws
  Else
        MsgBox "Incorrect Password", vbCritical, "Incorrect Password"
  End If
Application.ScreenUpdating = True
End Sub

Thanks in advance!!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi, you could try adding this line directly after the strpwd = inputbox(.. line of code.

If strPwd = "" Then Exit Sub
 
Upvote 0
Hi, that really doesn't give us a lot to go on to help you troubleshoot!

Can you post your amended code?

Code:
 Sub UnProtectAll()
 
     'Use Ctl+Shift+U To Unlock All Sheets
     
    Dim ws As Worksheet
    Dim strPwd As String
    Dim strCheck As String
Application.ScreenUpdating = False
   strCheck = "mypassword"
     strPwd = InputBox("Enter Password", "Password", "Enter Password")
If strPwd = "" Then Exit Sub
  For Each ws In ThisWorkbook.Worksheets
     ws.Unprotect Password:=strPwd
  Next ws
  Else
        MsgBox "Incorrect Password", vbCritical, "Incorrect Password"
  End If
Application.ScreenUpdating = True
End Sub

I errors out right away with the modified "If strPwd = strCheck Then"
 
Upvote 0
I errors out right away with the modified "If strPwd = strCheck Then"

Hi, I didn't suggest that you modify any lines of code - I suggested that you should add a new line.
 
Upvote 0
Yep, that worked. Sorry for not understanding the first reply. Thank You!!!
 
Upvote 0

Forum statistics

Threads
1,216,176
Messages
6,129,319
Members
449,501
Latest member
Amriddin

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