You can use VBA in Excel to store a password in an encrypted form rather than as plain text, by running the string through a hashing or scrambling routine before you save it. The simplest reliable approach is a small VBA module that turns the password into a hash using a key you supply, then compares the hash of what the user types against the stored hash. Excel's built-in VBA has no dedicated encryption function, so you build the routine yourself using the Windows cryptographic API or a self-written cipher. This is fine for protecting a password from casual snooping inside your own workbook; it is not a substitute for proper credential management, because anyone who can open the VBA project can read your code and keys.
What You'll Need
- Excel on Windows with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer) or on Mac with the Developer tab shown in Preferences.
- A macro-enabled workbook saved as .xlsm, since macros cannot be stored in a standard .xlsx file.
- Macro security set to allow the workbook to run: File > Options > Trust Center > Trust Center Settings > Macro Settings. Choose "Disable all macros with notification" so you can enable your own file deliberately rather than trusting everything.
- A password you control that is long enough to be worth protecting, and a separate key or passphrase if you are using a keyed hash.
- Basic comfort with the VBA editor (Alt+F11 on Windows, or Tools > Macro > Visual Basic Editor on Mac). Menu names vary slightly between Excel versions and between Windows and Mac.
Step-by-Step
- Open the VBA editor. Press Alt+F11 on Windows, or use Tools > Macro > Visual Basic Editor on Mac. In the Project pane on the left, right-click your workbook and choose Insert > Module. A blank code window opens.
- Decide what you are protecting. Encryption in VBA is only useful if it stops someone reading a password in the sheet or in a hidden cell. If the goal is to stop someone editing a sheet, use sheet protection instead (Review > Protect Sheet) - that is a different mechanism and does not encrypt anything. Write down which one you actually need before coding.
- Choose a method. For real protection use the Windows cryptography API through a Declare statement (Windows only) or a well-known hashing function such as SHA-256. For simple obfuscation inside a workbook you can write a keyed substitution or XOR routine in pure VBA, which also works on Mac. A plain XOR or Caesar shift is easy to reverse and should be treated as a speed bump, not security.
- Add a hash function. For a pure VBA example, paste a keyed routine into the module. The idea: convert each character of the password to its character code, combine it with the corresponding character of your key using
Xor, then convert the result back into a hex string. Every character in, one pair of hex digits out. For example, a loop overLen(password)that doesresult = result & Right("0" & Hex(Asc(Mid(password, i, 1)) Xor Asc(Mid(key, ((i - 1) Mod Len(key)) + 1, 1))), 2)produces a fixed-length scrambled string. Because the result is deterministic, the same password and key always give the same hash. - Store the hash, never the password. Put the stored hash in a cell or a module-level constant. If you use a cell, hide the column and protect the sheet, but understand that hiding is not encryption - the hash itself is what protects the password, and hiding the hash only slows someone down.
- Write the checking routine. A small Sub asks for input with
InputBox, passes it through the same hash function with the same key, and compares the result to the stored hash using a string comparison. If they match, allow the next action; if not, show a message and exit. Keep the comparison exact and case-sensitive, because a hash is a fingerprint - one changed character produces a completely different result. - Test with a throwaway password. Run the Sub, type the correct password, and confirm it passes. Then type a wrong one and confirm it fails. Type one with a different capitalisation and confirm it fails. Test an empty input too, since an empty string still hashes to something and could accidentally match if your stored value was also empty.
- Remove your test values. Delete any debug
MsgBoxlines that display the hash or the password, because they leak the very thing you are protecting. Save as .xlsm, close the workbook, reopen it, and confirm the macro still runs when you enable content. - Lock the VBA project if you want a second layer. In the VBA editor choose Tools > VBAProject Properties, go to the Protection tab, tick "Lock project for viewing" and set a password. This stops casual reading of your source code and your key, but it is not unbreakable and should not be relied on as the only defence.
Common Mistakes
The most frequent error is storing the plain password somewhere "clever" such as a hidden worksheet, a defined name, or a comment - all of which are trivially readable by anyone who knows where to look. The second is using a fixed key written directly in the same module as the hash function: it works, but the key is visible to anyone who unlocks the project, so treat it as a convenience layer only.
Another common slip is comparing hashes with a case-insensitive comparison or trimming whitespace before hashing. If the user's password genuinely contains a space or a capital letter, trimming or lowercasing it changes the hash and the password is rejected or, worse, a different password is accepted. Do not normalise the input beyond what your rules explicitly say.
People also forget that a .xlsx workbook silently drops macros on save. If the check stops working after a save, confirm the file is still .xlsm. Finally, a hash routine that uses Asc on characters outside the basic Latin range can behave differently across locales; if users may type accented or non-Latin characters, test with those characters explicitly.
Tips
Keep the hash function in one place and call it from every procedure that needs to check a password, so a change to the algorithm or key is made once. Put a short comment above it stating the method and key length so a future editor understands what the stored hex string represents. Use a long key - sixteen characters or more - and never reuse a key that protects something more important.
If you need stronger protection on Windows, use the cryptographic API rather than hand-written XOR. It is a little more code (a Declare statement pointing at the hash functions in advapi32), but it gives you SHA-256 or better without inventing your own arithmetic. On Mac, that API is not available, so a pure VBA routine is the practical choice and should be described to users as obfuscation rather than encryption.
Add a delay or a limited number of attempts to the check routine. A loop that lets someone try endlessly will eventually succeed against a short or guessable password, no matter how good the hash is. Three attempts followed by closing the workbook is a reasonable baseline.
Troubleshooting
The macro will not run. Check that the file is .xlsm and that macro security is not set to "Disable all macros without notification". If the workbook came from another computer, Windows may have blocked it: right-click the file, open Properties, and clear the "Unblock" checkbox if it is shown. On a managed work machine, group policy may block macros entirely and settings will be greyed out - in that case you cannot change it yourself.
Compile errors. A red-highlighted line usually means a missing reference or a typo in a variable name. Choose Debug > Compile VBAProject to see the first error; fix from the top down, because one error often causes several others. Add Option Explicit at the top of the module to catch undeclared variables.
Wrong password accepted or right password rejected. Print both the expected and the produced hash to the Immediate window during testing (Debug.Print) and compare them character by character. Look for a leading zero lost in the hex conversion, a trailing space in the stored cell, or a key that differs by case from the one used when the hash was generated.
The check works once then stops. If your routine clears the stored value or overwrites a cell with the hash each run, the second run compares against the wrong value. Keep the checking code read-only and generate the stored hash in a separate, deliberate step.
Frequently Asked Questions
Does this make my password truly secure?
Not against someone determined. VBA code and workbook contents can be read by anyone with access to the file and the tools to inspect it. Treat this as protection against casual viewing, and use a proper password manager for credentials that matter.
Can I decrypt the stored value to show the original password?
With a hash, no - that is the point. A hash is one-way, so you can only check a guess. If you write a reversible cipher you can decrypt it, but then anyone who finds the key can do the same.
Does the same code work on Excel for Mac?
The pure VBA approach does. Calls to the Windows cryptography API do not work on Mac. Menu names in the VBA editor also differ slightly, so follow the Mac wording shown in the editor.
Where should I store the hash?
Either in a protected worksheet cell or as a constant in the VBA module. A cell is easier to update; a constant is slightly harder to stumble across. Neither is encrypted, so rely on the hash function, not on hiding.
Can I use this to protect a whole workbook?
No. Workbook-level protection uses File > Info > Protect Workbook and its own password, which is a separate esata-aint feature and equally not a security boundary. Use VBA password checks only for your own in-sheet checks.
Why does my hash differ between two computers?
Almost always a key difference or a locale issue in character conversion. Make sure both machines use the same key string and test with plain ASCII characters first.