Are you spending 30–60 minutes every day creating the same report — copying data, formatting cells, applying filters, and saving files? If yes, VBA Macros can give you that time back.
VBA (Visual Basic for Applications) is Excel's built-in programming language. With it, you can record and automate any repetitive task — and run it with a single click. You do not need to be a programmer to get started.
In this article, you will learn — step by step — how to record your first macro, understand the code it generates, edit it, and use it to automate your daily reports.
What is a VBA Macro?
A Macro is a recorded sequence of actions in Excel. When you record a macro, Excel watches what you do and writes the VBA code for you automatically. You can then replay that recorded sequence any time — instantly.
Think of it like this: instead of doing 20 steps manually every morning, you press one button and Excel does all 20 steps for you in under 3 seconds.
Before You Start — Enable the Developer Tab
The Developer tab is hidden by default. You need it to record and run macros.
Enable the Developer Tab
- Click File → Options
- Click Customize Ribbon on the left
- On the right panel, find Developer in the list
- Check the box next to it
- Click OK
The Developer tab will now appear in your Ribbon between View and Help.
Step 1 — Record Your First Macro
The fastest way to create a macro is to simply record what you do. Excel writes the code for you.
Recording a Macro in Excel
- Click the Developer tab
- Click Record Macro
- Give your macro a name — e.g.,
FormatDailyReport(no spaces allowed) - Assign a shortcut key — e.g., Ctrl + Shift + R
- Choose where to store it: This Workbook (recommended)
- Click OK — recording starts now
- Perform your actions: format headers, apply bold, add borders, etc.
- When done, click Stop Recording on the Developer tab
Your macro is saved. Press the shortcut key to replay it instantly.
Step 2 — View and Understand the VBA Code
After recording, Excel generates VBA code automatically. Let us open it and understand what it looks like.
Open the VBA Editor
- Press Alt + F11 — this opens the VBA Editor (also called VBE)
- In the left panel, expand your workbook
- Double-click Module1 — your recorded macro code appears
Here is what a typical recorded macro looks like — let us say you recorded formatting a header row:
' This macro formats the header row of your report
Sub FormatDailyReport()
' Select the header row
Range("A1:F1").Select
' Apply bold formatting
Selection.Font.Bold = True
' Set background colour to red
Selection.Interior.Color = RGB(239, 68, 68)
' Set font colour to white
Selection.Font.Color = RGB(255, 255, 255)
' Set font size to 12
Selection.Font.Size = 12
' Auto-fit all columns
Cells.EntireColumn.AutoFit
End Sub
Every macro starts with Sub MacroName() and ends with End Sub. Everything in between is the list of instructions Excel follows.
•
Range("A1:F1").Select — selects cells A1 to F1•
Selection.Font.Bold = True — makes the selected cells bold•
RGB(239, 68, 68) — sets a red colour using Red, Green, Blue values•
Cells.EntireColumn.AutoFit — auto-fits all column widths
Step 3 — Write a Macro from Scratch
Now let us write a real, practical macro manually. This one will automate a daily report — it will format data, add a date stamp, and save a copy automatically.
Daily Report Automation Macro
This macro does 6 things automatically:
- Formats the header row
- Auto-fits all columns
- Adds today's date in cell H1
- Sorts data by column B (Sales) — highest first
- Saves a copy with today's date in the file name
- Shows a confirmation message
Sub AutomateDailyReport()
'--- Step 1: Format the header row ---
With Range("A1:F1")
.Font.Bold = True
.Font.Size = 12
.Font.Color = RGB(255, 255, 255)
.Interior.Color = RGB(239, 68, 68)
.HorizontalAlignment = xlCenter
End With
'--- Step 2: Auto-fit all columns ---
Cells.EntireColumn.AutoFit
'--- Step 3: Add today's date in H1 ---
Range("H1").Value = "Report Date: " & Format(Now(), "DD-MMM-YYYY")
Range("H1").Font.Bold = True
'--- Step 4: Sort by Sales column (B) highest first ---
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("A1:F" & lastRow).Sort _
Key1:=Range("B1"), _
Order1:=xlDescending, _
Header:=xlYes
'--- Step 5: Save a copy with today's date ---
Dim savePath As String
savePath = ThisWorkbook.Path & "\" & _
"DailyReport_" & Format(Now(), "YYYY-MM-DD") & ".xlsx"
ThisWorkbook.SaveCopyAs savePath
'--- Step 6: Show confirmation message ---
MsgBox "✅ Report ready! Saved as: " & savePath, _
vbInformation, "Daily Report Done"
End Sub
Cells(Rows.Count, "A").End(xlUp).Row finds the last row with data automatically — so your macro works no matter how many rows your data has. This is one of the most useful VBA tricks for dynamic data.
Step 4 — Run Your Macro
There are 4 ways to run a macro in Excel:
| Method | How to Do It | Best For |
|---|---|---|
| Keyboard Shortcut | Press the shortcut you assigned (e.g. Ctrl+Shift+R) | Daily use — fastest |
| Developer Tab | Developer → Macros → Select → Run | Running any saved macro |
| Button on Sheet | Insert → Shapes → right-click → Assign Macro | Sharing with non-technical users |
| VBA Editor | Alt+F11 → click inside Sub → press F5 | Testing during development |
Step 5 — Add a Button to Run the Macro
Create a Macro Button on Your Sheet
- Go to Developer tab → Insert → Button (Form Control)
- Draw the button on your sheet by clicking and dragging
- The Assign Macro dialog opens automatically
- Select your macro name → click OK
- Right-click the button → Edit Text → rename it: "Generate Report"
Now anyone can click this button to run the macro — no knowledge of VBA needed.
Step 6 — Loop Through Data Automatically
One of VBA's most powerful features is looping — going through each row and doing something. Here is a practical loop that highlights rows where sales are below target:
Sub HighlightBelowTarget()
Dim i As Long
Dim lastRow As Long
Dim target As Long
target = 50000 ' Set your sales target here
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
' Loop through each data row (starting from row 2)
For i = 2 To lastRow
If Cells(i, 2).Value < target Then
' Highlight the entire row in light red
Rows(i).Interior.Color = RGB(255, 220, 220)
Else
' Clear any existing colour for rows above target
Rows(i).Interior.ColorIndex = xlNone
End If
Next i
MsgBox "Done! Rows below ₹50,000 target are highlighted.", _
vbInformation
End Sub
Step 7 — Schedule Your Macro to Run Automatically
You can make a macro run automatically when you open the workbook — so your report is ready the moment you open the file every morning.
' This goes in the "ThisWorkbook" module — NOT in a Module
' Double-click "ThisWorkbook" in the VBA editor to find it
Private Sub Workbook_Open()
' This runs automatically every time the file is opened
Call AutomateDailyReport
End Sub
•
Workbook_BeforeClose — runs before the file closes•
Workbook_BeforeSave — runs before every save•
Worksheet_Change — runs when any cell value changes
Common VBA Mistakes to Avoid
- Saving as .xlsx instead of .xlsm — All macros are deleted. Always save as .xlsm.
- Using Select and Selection everywhere — Recorded macros use Select a lot. Professional VBA avoids it. Instead of
Range("A1").Select / Selection.Bold = True, writeRange("A1").Bold = Truedirectly. - Hardcoding the last row number — Never write
Range("A1:A500"). UseCells(Rows.Count,"A").End(xlUp).Rowto find it dynamically. - Not handling errors — If something goes wrong, the macro crashes. Add
On Error GoTo ErrorHandlerat the top of important macros. - No comments in code — Always add comments (lines starting with ') to explain what each section does. Future you will thank present you.
🎯 Summary — Your VBA Automation Checklist
Here is everything you need to automate your daily reports with VBA:
- Enable the Developer tab — File → Options → Customize Ribbon
- Record a macro — Developer → Record Macro → do your steps → Stop
- Open the VBA editor — Alt+F11 to view and edit the code
- Edit the recorded code — Clean it up, remove unnecessary Select statements
- Write your own macro — Format, sort, save, loop through data
- Add a button — Developer → Insert → Button → Assign Macro
- Auto-run on open — Put code in ThisWorkbook → Workbook_Open
- Save as .xlsm — Never .xlsx for files with macros
Frequently Asked Questions
Want to master VBA from scratch?
Our VBA & Macros Basics course covers everything — from your first macro to building full automation tools — step by step with real projects.