What Most People Miss About How to Apply VBA Code in Excel
By Sarah Mitchell
Yes, you can apply VBA code in Excel. But if you paste it into a module and expect it to run automatically on open, you’ve just created a silent failure.
The Problem
Your finance team sends you a raw payroll dump every Friday. It arrives as a flat list — no formatting, no totals, inconsistent date formats, and blank rows scattered like confetti. You manually insert columns, apply conditional highlighting, sum overtime, and copy-paste results into a report. Last week, Sarah Chen missed two entries because row 47 was hidden behind a filter she didn’t notice. This isn’t inefficiency — it’s risk.
Here’s what the raw data actually looks like in Sheet1 (A1:E12):
Employee
Dept
Base Pay
Overtime
Date
Liu Wei
Logistics
$3,200
$420
2024-03-15
Maria Lopez
Sales
$4,100
$0
2024-03-15
James Park
IT
$5,800
$695
15-Mar-24
Anya Dubois
HR
$3,900
$210
2024/03/15
Rajiv Mehta
Logistics
$3,200
$0
2024-03-15
Tina Zhang
Sales
$4,450
$380
15-Mar-24
Diego Santos
IT
$5,600
$0
2024/03/15
Nina Okoro
HR
$4,100
$120
2024-03-15
Markus Vogel
Logistics
$3,200
$0
2024-03-15
Notice the blanks, mixed date formats, and missing headers? That’s why your SUM formulas break. And no — AutoFill won’t fix this. You’re not lazy. You’re just stuck in manual mode.
The Solution
This is how to apply VBA code in Excel *without* saving as macro-enabled or trusting the ribbon. Do these four things in order:
1. Press Alt + F11 — that opens the VBA editor. Don’t click anything else yet.
2. In the Project Explorer (left pane), right-click VBAProject (YourWorkbookName.xlsx) → Insert → Module. A new Module1 appears.
3. Paste this exact code into Module1 — no editing needed yet:
Sub CleanPayroll()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Range("A1:E1").AutoFilter
ws.Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
ws.Columns("E:E").NumberFormat = "yyyy-mm-dd"
ws.Range("F1").Value = "Total Pay"
ws.Range("F2:F" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row).Formula = "=C2+D2"
End Sub
4. Go back to Excel (Alt + Q closes VBA editor), then press Alt + F8, select CleanPayroll, and click Run.
That’s it. No macros enabled warning. No file format change. Just clean output — instantly.
Here’s what Sheet1 looks like after running the macro (A1:F11):
Employee
Dept
Base Pay
Overtime
Date
Total Pay
Liu Wei
Logistics
$3,200
$420
2024-03-15
$3,620
Maria Lopez
Sales
$4,100
$0
2024-03-15
$4,100
James Park
IT
$5,800
$695
2024-03-15
$6,495
Anya Dubois
HR
$3,900
$210
2024-03-15
$4,110
Rajiv Mehta
Logistics
$3,200
$0
2024-03-15
$3,200
Tina Zhang
Sales
$4,450
$380
2024-03-15
$4,830
Diego Santos
IT
$5,600
$0
2024-03-15
$5,600
Nina Okoro
HR
$4,100
$120
2024-03-15
$4,220
Markus Vogel
Logistics
$3,200
$0
2024-03-15
$3,200
Going Further
Want it to run automatically when the file opens? Add this to ThisWorkbook (not Module1):
Private Sub Workbook_Open()
Call CleanPayroll
End Sub
But here’s what most people miss: If your file is opened from SharePoint or Teams, auto-run macros are blocked by default. Instead, assign the macro to a shape: Insert → Shapes → Rectangle → right-click → Assign Macro → CleanPayroll. Now anyone — even non-VBA users — clicks once and it runs.
You can also trigger it from a cell: type =CLEANPAYROLL() in any cell? Nope. That doesn’t work. But you *can* use a custom function. Paste this into Module1:
Function TotalPay(base As Double, ot As Double) As Double
TotalPay = base + ot
End Function
Then use =TotalPay(C2,D2) anywhere — no macro security warnings, works in .xlsx files.
When NOT to Use This
Don’t apply VBA code in Excel for one-off tasks that take less than 90 seconds manually. Seriously — if you only do it once a month, record a Quick Access Toolbar macro instead (View → Macros → Record Macro).
Avoid VBA entirely if your workbook gets shared with Mac users. The SpecialCells(xlCellTypeBlanks) line crashes Excel for Mac — use For Each r In Range(...) loops instead.
Also skip VBA if your data lives in Power Query. You’ll spend more time debugging VBA than rebuilding the logic in M. One exception: if you need to write values back to cells (e.g., timestamping edits), VBA still wins.
And never paste VBA from random forums without checking for On Error Resume Next — that line hides critical errors. Delete it before running.
Keyboard Shortcuts
Action
Shortcut
Open VBA Editor
Alt + F11
Return to Excel from VBA
Alt + Q
Run Macro Dialog
Alt + F8
Insert New Module
Alt + I, M
Save Module Code
Ctrl + S
Debug Immediate Window
Ctrl + G
Sarah Mitchell
Sarah has 12 years of experience covering Microsoft 365 productivity tools and enterprise software workflows. She specializes in Excel automation and SharePoint integration.