Do you repeat the same Excel steps every day or every week? Formatting the same report, copying data between sheets, creating the same summary, emailing the same file? VBA can do all of that for you with one click. This Excel VBA tutorial is written for complete beginners. You do not need any programming experience. By the end you will have written real macros that automate actual office tasks.
Table of Contents
- What Is VBA in Excel?
- Step 1: Show the Developer Tab
- Step 2: Save Your File as Macro-Enabled
- Step 3: Open the VBA Editor
- Step 4: Write Your First Macro
- Step 5: Work With Cells and Ranges
- Step 6: Variables
- Step 7: Make Decisions With IF
- Step 8: Repeat Actions With Loops
- Step 9: A Real Automation Project
- Step 10: Run Macros With a Button
- Step 11: Handle Errors
- Step 12: Create Your Own Excel Function
- Debugging Tips for Beginners
- Recording Macros: A Shortcut for Learning
- Your Excel VBA Tutorial Learning Roadmap
- Frequently Asked Questions
- Conclusion
What Is VBA in Excel?
VBA stands for Visual Basic for Applications. It is the programming language built into Microsoft Office. With VBA you can:
- Automate repetitive tasks (formatting, copying, cleaning, reporting)
- Create custom functions that Excel does not have
- Build forms for data entry
- Loop through hundreds of files or sheets
- Control other Office apps such as Outlook and Word
A macro is simply a VBA program that performs a task. When people say “write a macro”, they mean “write some VBA code.”
Is VBA still relevant?
Yes. Millions of business workbooks rely on VBA, it runs in desktop Excel on Windows and Mac without installing anything, and companies still advertise for people with VBA skills. Newer tools such as Power Query, Office Scripts and Python exist, but VBA remains the most direct way to automate desktop Excel. Read more in is learning Excel VBA still worth it.
Step 1: Show the Developer Tab
The Developer tab is hidden by default.
Windows: File > Options > Customize Ribbon > tick Developer > OK.
Mac: Excel > Preferences > Ribbon & Toolbar > tick Developer.
The Developer tab contains the Visual Basic editor, macro recording and form controls.
Step 2: Save Your File as Macro-Enabled
Normal .xlsx files cannot store macros. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). If you forget, Excel warns you and your code is lost when you close the file.
Macro security
When you open a file with macros, Excel may show a yellow security bar. Click Enable Content only for files you trust. Files downloaded from the internet may be blocked entirely. If so, right-click the file in Windows Explorer, choose Properties and tick Unblock.
Step 3: Open the VBA Editor
Press Alt + F11 (Windows) or Option + F11 / Fn + Option + F11 (Mac). The Visual Basic Editor (VBE) opens. Key parts:
| Part | Purpose |
|---|---|
| Project Explorer | Lists open workbooks, their sheets and modules |
| Code window | Where you write code |
Immediate window (Ctrl + G) |
Test single lines and print results |
Properties window (F4) |
Settings of the selected object |
Insert a module: Insert > Module. Modules are where most of your macros live.
Step 4: Write Your First Macro
Type this in the new module:
Sub HelloWorld()
MsgBox "Hello! My first VBA macro is working."
End Sub
Click inside the code and press F5. A message box appears. Congratulations, you are now a VBA programmer.
Every macro starts with Sub Name() and ends with End Sub. Names cannot contain spaces.
Step 5: Work With Cells and Ranges
Most VBA work is reading and writing cells.
Sub WorkWithCells()
Range("A1").Value = "Sales Report"
Range("A1").Font.Bold = True
Range("A1").Font.Size = 16
Range("B2:B10").Value = 0
Cells(3, 2).Value = 500 ' Row 3, Column 2 = B3
Range("A1:E1").Interior.Color = RGB(220, 230, 241)
Columns("A:E").AutoFit
End Sub
Lines starting with an apostrophe ' are comments. Excel ignores them; they explain your code.
Refer to a specific sheet
Worksheets("Data").Range("A1").Value = "Updated"
Always naming the sheet is safer than relying on whichever sheet happens to be active.
Find the last row of data
This is one of the most important lines in VBA, because data length changes every time:
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
It starts at the very bottom of column A and jumps up to the last filled cell, just like pressing Ctrl + Up Arrow.
Learn VBA from beginner to advanced
Go from your first macro to full automation projects with UserForms, loops, error handling and real business examples.
Step 6: Variables
Variables store values while your macro runs.
Sub UseVariables()
Dim customerName As String
Dim quantity As Long
Dim price As Double
Dim total As Double
customerName = Range("A2").Value
quantity = Range("B2").Value
price = Range("C2").Value
total = quantity * price
Range("D2").Value = total
MsgBox customerName & " owes " & Format(total, "#,##0.00")
End Sub
Common data types:
| Type | Stores |
|---|---|
String |
Text |
Long |
Whole numbers (use instead of Integer) |
Double |
Decimal numbers |
Boolean |
True / False |
Date |
Dates and times |
Range |
A cell or group of cells |
Worksheet |
A sheet |
Add Option Explicit at the top of each module. It forces you to declare variables and catches spelling mistakes. Turn it on permanently in Tools > Options > Require Variable Declaration.
Step 7: Make Decisions With IF
Sub CheckTarget()
If Range("B2").Value >= Range("C2").Value Then
Range("D2").Value = "Target met"
Range("D2").Interior.Color = vbGreen
ElseIf Range("B2").Value >= Range("C2").Value * 0.8 Then
Range("D2").Value = "Close"
Range("D2").Interior.Color = vbYellow
Else
Range("D2").Value = "Below target"
Range("D2").Interior.Color = vbRed
End If
End Sub
Step 8: Repeat Actions With Loops
Loops are where VBA saves the most time. This macro checks every row, however many there are:
Sub MarkOverdueInvoices()
Dim i As Long, lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If Cells(i, "E").Value <> "Paid" And Cells(i, "D").Value < Date Then
Cells(i, "F").Value = "Overdue"
Rows(i).Font.Color = vbRed
End If
Next i
End Sub
Loop through every worksheet in the workbook:
Sub FormatAllSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("A1").Font.Bold = True
ws.Columns.AutoFit
Next ws
End Sub
Learn every type of loop in Excel VBA loops explained.
Step 9: A Real Automation Project
Here is a complete macro that cleans and formats a raw data export, a task many people do by hand every week:
Sub CleanSalesReport()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Worksheets("Data")
Application.ScreenUpdating = False
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Trim spaces and fix capitalization in customer names
For i = 2 To lastRow
ws.Cells(i, "B").Value = Application.WorksheetFunction.Proper(Trim(ws.Cells(i, "B").Value))
Next i
' Format headers
With ws.Range("A1:F1")
.Font.Bold = True
.Interior.Color = RGB(31, 78, 121)
.Font.Color = vbWhite
End With
' Number formats
ws.Range("E2:E" & lastRow).NumberFormat = "#,##0.00"
ws.Range("A2:A" & lastRow).NumberFormat = "dd-mmm-yyyy"
' Add a total row
ws.Cells(lastRow + 1, "D").Value = "Total"
ws.Cells(lastRow + 1, "E").Formula = "=SUM(E2:E" & lastRow & ")"
ws.Rows(lastRow + 1).Font.Bold = True
ws.Columns("A:F").AutoFit
Application.ScreenUpdating = True
MsgBox "Report cleaned: " & lastRow - 1 & " rows processed."
End Sub
Application.ScreenUpdating = False stops the screen from flickering and makes macros run much faster.
Step 10: Run Macros With a Button
- Go to Developer > Insert > Button (Form Control).
- Draw the button on the sheet.
- Choose the macro to assign and click OK.
- Right-click the button to edit its text, for example “Clean Report”.
Now anyone can run the automation with one click. You can also assign macros to shapes or add them to the Quick Access Toolbar.
Step 11: Handle Errors
Without error handling, a macro stops with a confusing message. Add a simple handler:
Sub SafeMacro()
On Error GoTo ErrHandler
Worksheets("Data").Range("A1").Value = "OK"
Exit Sub
ErrHandler:
MsgBox "Something went wrong: " & Err.Description, vbExclamation
End Sub
Find solutions to the most common problems in Excel VBA errors and how to fix them.
Step 12: Create Your Own Excel Function
VBA can create functions you use in cells like built-in ones:
Function VATAmount(amount As Double, Optional rate As Double = 0.15) As Double
VATAmount = Round(amount * rate, 2)
End Function
In any cell: =VATAmount(B2) or =VATAmount(B2, 0.075).
Debugging Tips for Beginners
- F8 runs your code one line at a time so you can watch what happens.
- F9 sets a breakpoint where the code pauses.
- Debug.Print writes values to the Immediate window:
Debug.Print lastRow. - Hover over a variable while paused to see its current value.
- Ctrl + Break stops a runaway loop.
Recording Macros: A Shortcut for Learning
The Macro Recorder writes VBA code while you click around Excel. It is a brilliant way to discover the code for a task you do not know how to write. Learn the technique, and how to clean up recorded code, in how to record a macro in Excel.
Your Excel VBA Tutorial Learning Roadmap
- Week 1: Developer tab, modules, MsgBox, Range, Cells, recording macros.
- Week 2: Variables, IF statements, finding the last row.
- Week 3: For and For Each loops across rows, sheets and workbooks.
- Week 4: Error handling, buttons, custom functions.
- Next: UserForms, working with files and folders, sending emails from Excel, and combining sheets.
Want ready-made code to study? See 20 useful Excel VBA macro examples.
Automate hours of Excel work every week
Learn VBA step by step with real projects: report automation, UserForms, loops, file handling and more.
Frequently Asked Questions
Is VBA hard to learn for beginners?
No. VBA is one of the easiest programming languages to start with because you see results immediately in Excel. Most beginners write useful macros within the first week.
What is the difference between a macro and VBA?
VBA is the programming language. A macro is a program written in VBA that performs a task. Recording a macro creates VBA code automatically.
Does VBA work on Mac?
Yes, Excel for Mac supports VBA, though a few Windows-specific features such as some ActiveX controls and certain file dialogs behave differently.
Does VBA work in Excel Online or Google Sheets?
No. VBA only runs in desktop Excel. Excel for the web uses Office Scripts (TypeScript), and Google Sheets uses Apps Script (JavaScript).
Can VBA damage my computer?
Macros from unknown sources can be harmful, which is why Excel disables them by default. Only enable macros in files you created or trust.
Conclusion
VBA turns Excel from a calculator into an automation tool. In this Excel VBA tutorial you enabled the Developer tab, wrote macros, worked with ranges, used variables, decisions and loops, built a real clean-up macro, added a button, handled errors and created a custom function. Pick one repetitive task you do every week and automate it this week. That first real win is what makes VBA click.
Become the automation expert on your team
Master Excel VBA from beginner to advanced with practical lessons and lifetime access.
Good to know: BUY INDIVIDUAL COURSE directly through this link gives you lifetime access, and it supports the instructor far more than watching it through a Udemy subscription plan.