Excel VBA validation means using a short macro to check what a user types before it is accepted, so bad data never reaches your sheet. The quickest working version is a Worksheet_Change event that tests the edited cell and either keeps the value or clears it with a message. You do not need add-ins or special permissions, only a macro-enabled workbook (.xlsm) and the Developer tab visible.
What You'll Need
- Excel for Windows or Mac with macros enabled (the steps below use the Windows ribbon; Mac labels are nearly identical).
- A workbook saved as .xlsm (Excel Macro-Enabled Workbook). Validation code is lost if you save as .xlsx.
- The Developer tab visible: right-click the ribbon and choose Customize the Ribbon, then tick Developer. Labels can differ slightly by version and region.
- A clear rule for what counts as valid: a date range, a list of allowed words, a number limit, or a pattern such as an email address.
Step-by-Step
- Open the right code module. Press Alt+F11 to open the Visual Basic Editor. In the Project pane, double-click the sheet you want to protect (for example Sheet1). Code placed in a sheet module only runs for that sheet; code in ThisWorkbook runs for the whole file. For a single data-entry sheet, use the sheet module.
- Start the event procedure. At the top of the blank code window, click the left dropdown and choose Worksheet, then the right dropdown and choose Change. Excel writes the empty
Private Sub Worksheet_Change(ByVal Target As Range)block for you. Do not rename it. - Limit the range first. Validation that fires on every cell is slow and annoying. Add a guard such as
If Intersect(Target, Range("B2:B500")) Is Nothing Then Exit Sub. This makes the macro ignore edits outside your data column. - Handle pasted blocks. A paste can change many cells at once, and
Target.Valuethen fails. AddIf Target.Cells.CountLarge > 1 Then Exit Subor loop throughTarget.Cells. Most simple setups just skip multi-cell edits and rely on Data Validation for pastes. - Write the test. For a numeric limit:
If Not IsNumeric(Target.Value) Or Target.Value < 0 Or Target.Value > 100 Then. For a required word list:If Application.WorksheetFunction.CountIf(Range("H2:H20"), Target.Value) = 0 Then. For a date window:If Not IsDate(Target.Value) Or Target.Value < Date Or Target.Value > Date + 30 Then. - Decide what happens on failure. The two safe choices are clearing the entry or undoing it. Clearing is simplest: turn off events, clear the cell, turn events back on, then show a message. Undoing needs
Application.Undoand works only for the last edit, so clearing is more reliable. - Disable events before you write to the sheet. Any change your macro makes to a cell re-triggers Worksheet_Change and can loop forever. Wrap writes in
Application.EnableEvents = Falseand set it back toTrueimmediately after, including in your error handler. - Add a message. Use
MsgBox "Enter a whole number from 0 to 100 in column B.", vbExclamationso the user knows the rule, not just that something failed. - Test every path. Type a valid value, an invalid value, a blank, text in a number field, and paste a block. Close and reopen the workbook to confirm the code persists and macros are enabled.
- Add worksheet protection only after testing. Protecting the sheet stops users from deleting your validation column, but it also blocks the macro from writing unless you unlock the cells it needs or unprotect and reprotect in code. Protect last, and keep the password recorded somewhere safe.
Common Mistakes
- Saving as .xlsx. The code vanishes on save with a warning most people click through. Always use .xlsm.
- Forgetting EnableEvents. The macro clears a cell, that change fires the event again, and Excel appears to freeze or the message box repeats endlessly. Press Esc and fix the toggles.
- Validating the whole sheet. Without an Intersect guard, every edit anywhere runs the test and the workbook slows down.
- Relying on VBA alone for pasted data. Worksheet_Change does not catch every multi-cell paste in the way beginners expect. Pair it with Data Validation (Data tab > Data Validation) for the same range.
- Testing only single cells. Real users paste, drag-fill and delete rows. Test those before sharing the file.
- Assuming the code runs on other machines. Macro security settings differ, and files from the internet are often blocked. Users may need to enable macros or unblock the file in its properties.
Tips
- Keep one small validation sub per rule and call it from Worksheet_Change, so the event code stays readable.
- Use named ranges (Formulas > Name Manager) instead of hard-coded addresses; the code survives inserted rows.
- Validate on entry, not on save, so users get feedback while they still remember what they typed.
- Store allowed values in a hidden sheet rather than inside the code; non-programmers can then update the list.
- Use
vbExclamationfor rule breaches andvbCriticalonly for real errors, so the two do not blur together. - Comment each rule with the business reason, not just the syntax.
Troubleshooting
Nothing happens when you type. Check the code is in the sheet module for that exact sheet, that the workbook is .xlsm, and that macros are enabled in the Trust Center. Also confirm your Intersect range includes the cell you edited.
The macro runs twice or loops. You are writing to a cell without turning events off. Add the EnableEvents toggles around every write, and reset them in the error handler.
Run-time error 13 (type mismatch). You compared text to a number. Test with IsNumeric or IsDate before comparing, and handle blanks explicitly.
Pasted data slips through. Add Data Validation to the same range, or loop over Target.Cells instead of skipping multi-cell edits.
The file is blocked on another PC. Right-click the file, open Properties, and tick Unblock if present; otherwise the user must allow macros in the Trust Center. Never ask anyone to lower macro security globally just for one file.
Protection stops the macro. Unprotect in code before writing and reprotect afterwards, or unlock the specific cells the macro touches.
Frequently Asked Questions
Can I validate without macros?
Yes. Data tab > Data Validation covers lists, whole numbers, decimals, dates and custom formulas, and it travels with the file without macro warnings. Use VBA only for rules Data Validation cannot express, such as cross-sheet checks or messages that depend on other cells.
Does Worksheet_Change catch every edit?
No. It fires on typed entries and most single-cell changes, but multi-cell pastes and some fill operations need extra handling. Combine it with Data Validation for full coverage.
Why does my macro clear the cell but the old value returns?
You are probably using Application.Undo, which only reverses the most recent action. Clear the cell directly instead, with events disabled.
Will the validation work on Excel for Mac?
The VBA event model is the same. Ribbon labels and file-blocking behaviour differ slightly, so test on the target machine before distributing.
Can I show a custom message instead of the default error?
Yes, with MsgBox in your failure branch. Data Validation also has its own Error Alert tab where you can set a title and message without any code.
Is VBA validation secure?
No. Anyone with access to the Visual Basic Editor can read or change the code, and worksheet protection is not encryption. Treat it as a data-quality aid, not a security control.