Skip to content
Free Excel Tutorials
  • Home
  • Excel For Beginners
  • Excel Intermediate
  • Advanced Excel For Experts

Data Analysis

  • Conditional Formatting New Rule with Formulas in Excel
  • Filter Data Based on Date in Excel
  • Understanding Pivot Tables in Excel
  • Get column index in Excel Table
  • Conditional Formatting Icon Sets Examples in Excel

References

  • Last row number in range
  • Approximate match with multiple criteria in Excel
  • How to use Excel LOOKUP Function
  • Create hyperlink with VLOOKUP in Excel
  • Complete List of Excel Lookup and Reference Functions, References and Examples

Data Validations

  • Excel Data validation require unique number
  • Excel Data validation allow weekday only
  • Excel Data validation with conditional list
  • How To Create Drop-down List in Excel
  • Excel Data validation specific characters only

Password Protect Macro in Excel

by

Just like you can password protect workbooks and worksheets, you can password protect a macro in Excel from being viewed (and executed).

Place a command button on your worksheet and add the following code lines:

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.

Worked Example:   Protect and Unprotect Worksheet in Excel

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”.

Worked Example:   Lock Cells in a Worksheet Excel

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:

Explanation: The macro uses the InputBox method of the Application object. If the users clicks Cancel, this method returns False and nothing happens (InputBox disappears). Only when the user knows the password (“easy” again), the secret code will be executed. If the entered password is incorrect, a MsgBox is displayed. Note that the user cannot take a look at the password in the Visual Basic Editor because the project is protected from being viewed.

Post navigation

Previous Post:

SUBSTITUTE function vs REPLACE function in Excel

Next Post:

Count cells between dates in Excel

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Learn Basic Excel

Ribbon
Workbook
Worksheets
Format Cells
Find & Select
Sort & Filter
Templates
Print
Share
Protect
Keyboard Shortcuts

Categories

  • Charts
  • Data Analysis
  • Data Validation
  • Excel Functions
    • Cube Functions
    • Database Functions
    • Date and Time Functions
    • Engineering Functions
    • Financial Functions
    • Information Functions
    • Logical Functions
    • Lookup and Reference Functions
    • Math and Trig Functions
    • Statistical Functions
    • Text Functions
    • Web Functions
  • Excel VBA
  • Excel Video Tutorials
  • Formatting
  • Grouping
  • Others

Logical Functions

  • OR function: Description, Usage, Syntax, Examples and Explanation
  • How to return blank in place of #DIV/0! error in Excel
  • XOR function: Description, Usage, Syntax, Examples and Explanation
  • NOT function: Description, Usage, Syntax, Examples and Explanation
  • Extract multiple matches into separate rows in Excel

Date Time

  • Get days, months, and years between dates in Excel
  • How to calculate months between dates in Excel
  • Convert Excel time to decimal seconds
  • Add workdays to date custom weekends in Excel
  • How to join date and text together in Excel

Grouping

  • Group numbers at uneven intervals in Excel
  • If cell contains one of many things in Excel
  • Group arbitrary text values in Excel
  • Categorize text with keywords in Excel
  • Group times into unequal buckets in Excel

General

  • How to calculate total from percentage in Excel
  • How to create dynamic named range with INDEX in Excel
  • How to fill cell ranges with random number from fixed set of options in Excel
  • How to calculate profit margin percentage in Excel
  • AutoFit Column Width, AutoFit Row Height in Excel
© 2023 xlsoffice . All Right Reserved. | Teal Smiles | Abbreviations And Their Meaning