Requirement:
The user has a worksheet that is protected/locked with a password.
The user has a CommandButton that, when clicked loads a UserForm that is used to enter records into the worksheet.
The user wants it in such a way that when the CommandButton is clicked, it will prompt the user to enter the unprotect password (used to protect/lock the worksheet), before the UserForm loads.
If the user enters the correct unprotect password, then the UserForm loads. If the user enters the incorrect unprotect password, a msgbox displays and the UserForm will not load.
The problem is when the CommandButton is clicked, the user will be prompted to enter the unprotect password (Excel's Built-In Unprotect dialog), if the user clicks the "Cancel" button, the UserForm still loads. This is not what is wanted - what is wanted is: If the "Cancel" button is clicked, the MsgBox displays, and the UserForm does NOT load. Below is the code for the CommandButton:
Private Sub CommandButton1_Click() On Error Resume Next ActiveSheet.Unprotect If Err <> 0 Then MsgBox: MsgBox "Incorrect Password. Unlock Failed!" Else UserForm3.Show End If End Sub
https://www.ozgrid.com/forum/forum/help-forums/excel-vba-macros/149357-unprotect-command-button
Solution:
Private Sub CommandButton1_Click() On Error GoTo ErrorOccured Dim pwd1 As String pwd1 = InputBox("Please Enter the password") If pwd1 = "" Then Exit Sub ActiveSheet.Unprotect Password:=pwd1 UserForm1.Show Exit Sub ErrorOccured: MsgBox "Sheets could not be UnProtected - Password Incorrect" Exit Sub End Sub
Obtained from the OzGrid Help Forum.
Solution provided by dotchijack.
See also: Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions and Index to new resources and reference sheets
See also:
How to align command button with ActiveCell |
How to click a button and name and create a new sheet and then copy |
How to align command button with ActiveCell |
How to create a macro button to put date in selected cell within specific column |
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.