Command Button:-
Place a "Command Button" on your worksheet and add the following code lines:
1. On the Developer tab, click Insert.
2. In the ActiveX Controls group, click Command Button.
3. Drag a command button on your worksheet.
Assign a Macro:-
To assign a macro (one or more code lines) to the command button, execute the following steps.
1. Right click CommandButton1 (make sure Design Mode is selected).
2. Click View Code.
The Visual Basic Editor appears.
3. Place your cursor between Private Sub CommandButton1_Click() and End Sub.
4. Add the code line shown below.
Protect Macro
1. First, create a simple macro that you want to protect.
Range("A1").Value = "This is secret code"
2. Next, click Tools, VBAProject Properties...
3. On the Protection tab, check "Lock project for viewing" and enter a password twice.
4. Click OK.
5. Save, close and reopen the Excel file. Try to view the code.
The following dialog box will appear:
You can still execute the code by clicking on the command button but you cannot view or edit the code anymore (unless you know the password). The password for the downloadable Excel file is "easy".
6. If you want to password protect the macro from being executed, add the following code lines:
Dim password As Variant
password = Application.InputBox("Enter Password", "Password Protected")
Select Case password
Case Is = False
'do nothing
Case Is = "easy"
Range("A1").Value = "This is secret code"
Case Else
MsgBox "Incorrect Password"
End Select
Result when you click the command button on the sheet:
Place a "Command Button" on your worksheet and add the following code lines:
1. On the Developer tab, click Insert.
2. In the ActiveX Controls group, click Command Button.
3. Drag a command button on your worksheet.
Assign a Macro:-
To assign a macro (one or more code lines) to the command button, execute the following steps.
1. Right click CommandButton1 (make sure Design Mode is selected).
2. Click View Code.
The Visual Basic Editor appears.
3. Place your cursor between Private Sub CommandButton1_Click() and End Sub.
4. Add the code line shown below.
Protect Macro
1. First, create a simple macro that you want to protect.
Range("A1").Value = "This is secret code"
2. Next, click Tools, VBAProject Properties...
3. On the Protection tab, check "Lock project for viewing" and enter a password twice.
4. Click OK.
5. Save, close and reopen the Excel file. Try to view the code.
The following dialog box will appear:
You can still execute the code by clicking on the command button but you cannot view or edit the code anymore (unless you know the password). The password for the downloadable Excel file is "easy".
6. If you want to password protect the macro from being executed, add the following code lines:
Dim password As Variant
password = Application.InputBox("Enter Password", "Password Protected")
Select Case password
Case Is = False
'do nothing
Case Is = "easy"
Range("A1").Value = "This is secret code"
Case Else
MsgBox "Incorrect Password"
End Select
Result when you click the command button on the sheet: