Why does your macro button do nothing when clicked? Why does Alt+F11 open a blank window with no modules? Why does Excel say 'Compile error: Sub or Function not defined' even though you copied the code from a trusted site?
Quick Answer
You don’t "input" VBA code like typing into a cell. You paste it into a specific location inside the Visual Basic Editor (VBE) — either a module, a worksheet object, or ThisWorkbook — and then run it using Alt+F8 or a button. If any of those locations aren’t open, visible, or enabled, the code stays inert.
All the Methods
| Method | Steps | Best For | Limitations |
|---|---|---|---|
| Standard Module (Insert > Module) | Alt+F11 → Insert → Module → Paste code → Alt+F8 → Run | Reusable macros (e.g., formatting reports, cleaning data) | Won’t trigger automatically on events (like opening or changing cells) |
| Worksheet Object (e.g., Sheet1) | Alt+F11 → Double-click 'Sheet1' in Project Explorer → Paste into white editor pane | Event-driven actions (e.g., highlight row when cell changes) | Only works for that sheet — copy/paste required for other sheets |
| ThisWorkbook Object | Alt+F11 → Double-click 'ThisWorkbook' → Paste → Save as .xlsm | Startup actions (e.g., auto-hide columns on open) | Fails silently if file isn’t saved as macro-enabled (.xlsm) |
| Personal Macro Workbook (PERSONAL.XLSB) | Record dummy macro → Store in 'Personal Macro Workbook' → Edit its module | Macros you want across all workbooks (e.g., quick currency conversion) | Hidden by default — must enable via View → Unhide in VBE |
Method 1 Deep Dive
Let’s say you got this VBA snippet from a colleague to auto-format sales rows:
Sub HighlightTopSales()
Dim rng As Range
Set rng = Range("A2:E10")
rng.AutoFilter Field:=3, Criteria1:=">=50000"
rng.SpecialCells(xlCellTypeVisible).Interior.Color = RGB(230, 240, 255)
End Sub
You try pasting it into cell A1. Nothing. That’s expected — Excel treats it as plain text.
Here’s what actually works:
- Press Alt+F11 to open the Visual Basic Editor
- In the Project Explorer (left panel), right-click VBAProject (YourFile.xlsm)
- Select Insert → Module. A new “Module1” appears.
- Click inside the white editor area and paste the code.
- Press Ctrl+S, then close the VBE.
- Back in Excel: Alt+F8 → Select HighlightTopSales → Run.
Now check your data — here’s what it transforms:
| Sales Rep | Region | Revenue | Status | Date |
|---|---|---|---|---|
| Sarah Chen | APAC | $62,400 | Active | 2024-03-15 |
| James Wu | EMEA | $38,900 | Active | 2024-03-12 |
| Maya Patel | Americas | $71,200 | Active | 2024-03-18 |
| Diego Morales | Americas | $29,500 | On Leave | 2024-03-10 |
The rows where Revenue ≥ $50,000 (Sarah and Maya) now have a light blue background — no manual filtering needed.
Counterintuitive tip: If Alt+F11 opens but shows no Project Explorer, press Ctrl+R. It’s often minimized or hidden — and nobody tells you that.
Method 2 Deep Dive
What if you want Excel to auto-highlight a row *the moment* someone types a value in column C? That’s an event — and it only lives in worksheet objects.
Open VBE (Alt+F11), then find Sheet1 under VBAProject (YourFile.xlsm) in the Project Explorer. Double-click it.
Paste this into the editor that opens:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C2:C20")) Is Nothing Then
Rows(Target.Row).Interior.Color = RGB(255, 250, 205)
End If
End Sub
This watches only cells C2:C20. Try typing "Yes" in C5 — the entire row 5 lights up yellow.
Important: This code will NOT run if you paste it into a regular module. It only works in a worksheet object — and only for that sheet. If you need it on Sheet2 too, double-click Sheet2 and paste the same code there.
Also — never name your worksheet tabs with spaces or special characters like “Q1 Results!” — Excel converts them to “Q1_Results_” behind the scenes, and your VBA may break without warning.
Cheat Sheet
| Action | Keyboard Shortcut | Where to Paste | File Format Required |
|---|---|---|---|
| Open Visual Basic Editor | Alt+F11 | N/A — just opens the environment | Any (but won’t save macros) |
| Show Project Explorer (if missing) | Ctrl+R | N/A | N/A |
| Run current macro | F5 (in VBE) or Alt+F8 (in Excel) | Must be in a module or sheet object | .xlsm or .xlsb |
| Save with macros enabled | F12 → Choose 'Excel Macro-Enabled Workbook (*.xlsm)' | N/A | Required — .xlsx strips all VBA |
| Unhide PERSONAL.XLSB | In VBE: View → Unhide → select PERSONAL.XLSB | Inside its Module1 | Already built-in — no extra save needed |