You have heard that VBA can automate your Excel tasks and save hours every week. But from where you stand right now, it looks like an insurmountable wall of code and programming. I promise you: in 15 minutes you will have created and run your first working macro.
I have taught VBA to hundreds of people over the years. Most of them are finance professionals, controllers and project managers – not programmers. They all share the same starting point: “I have never programmed anything.” And they all succeed in creating their first macro during the first lesson.
This guide takes you through exactly the same process. No prior knowledge is needed. Just Excel and 15 minutes of your time.
What you will create
In this tutorial we build a simple but useful macro that: 1. Formats header rows automatically (bold, coloured background) 2. Adjusts column widths so all text is visible 3. Adds today’s date to a cell
It may sound simple, but this macro contains the fundamentals of all VBA work. The same principles apply whether you are building a small formatting macro or a complex reporting system.
Preparation – Enable the Developer tab
Before we can start coding we need to see the VBA tools in Excel.
Step 1: Open Excel and a blank worksheet
Step 2: Click File in the menu
Step 3: Select Options at the bottom of the menu
Step 4: In the dialogue box that opens, click Customize Ribbon
Step 5: In the right-hand column you will find a list of tabs. Tick the box next to “Developer”
Step 6: Click OK
Now you see a new tab in the Excel menu called Developer. This is where all the tools for macros and VBA live.
Your first code – Hello World
Every programmer starts with “Hello World” – a simple program that just shows a message. We will do the same.
Open the VBA editor
There are two ways to open the VBA editor:
Option 1: Press Alt + F11 (this is the keyboard shortcut every VBA developer uses)
Option 2: Go to the Developer tab and click “Visual Basic”
The VBA editor opens in a new window. Don’t be intimidated by all the panes – focus on the middle, where the code is written.
Create a module
Code is written in modules. Think of them as containers for your macros.
Step 1: In the VBA editor, click Insert in the menu
Step 2: Select Module
A new blank window opens with the title “Module1” at the top. This is your code area.
Write your first macro
Type in the following code exactly as it appears here:
Sub MittFörstaMakro()
MsgBox "Grattis! Du har skapat ditt första VBA-makro."
End Sub
What does the code mean?
Sub MittFörstaMakro()– Starts a subroutine (a macro) with the name “MittFörstaMakro”MsgBox– The command for showing a message box- The text within quotation marks is the message that is displayed
End Sub– Ends the macro
Run your macro
Now let’s see it in action.
Option 1: Press F5 (run)
Option 2: Click the green play button in the toolbar
Option 3: Click Run > Run Sub/UserForm in the menu
A message box pops up with the text “Congratulations! You have created your first VBA macro.”
Click OK to close it.
You have just written and run VBA code. Feels good? It should. The same fundamental process applies to all macros, no matter how complex they get.
Do something useful – Format cells
Now we’ll build something that actually helps you in your daily Excel work.
Prepare your worksheet
Before we write the code we need some test data.
Step 1: Close the VBA editor (press Alt + Q or click the cross)
Step 2: In Excel, type the following in your worksheet:
- Cell A1: Product name
- Cell B1: Sales
- Cell C1: Cost
- Cell D1: Profit
Step 3: Add some example data under the headers:
- Row 2: Product A, 10000, 6000, 4000
- Row 3: Product B, 15000, 9000, 6000
Now we have something to work with.
Write the formatting macro
Open the VBA editor again (Alt + F11).
Below your first macro (leave a little space) you write this new macro:
Sub FormateraRubrik()
' Formatera rubrikraden (rad 1)
Range("A1:D1").Font.Bold = True
Range("A1:D1").Interior.Color = RGB(68, 114, 196)
Range("A1:D1").Font.Color = RGB(255, 255, 255)
Range("A1:D1").HorizontalAlignment = xlCenter
' Anpassa kolumnbredder
Columns("A:D").AutoFit
' Lägg till dagens datum
Range("F1").Value = "Genererad: " & Date
MsgBox "Formatering klar!"
End Sub
What does the code do?
Let’s break down each part:
Range("A1:D1").Font.Bold = True
This takes the cell range A1 to D1 and makes the text bold.
Range("A1:D1").Interior.Color = RGB(68, 114, 196)
Changes the background colour to blue. RGB stands for Red-Green-Blue and the numbers specify the colour mix.
Range("A1:D1").Font.Color = RGB(255, 255, 255)
Makes the text white (255,255,255 is white in RGB).
Range("A1:D1").HorizontalAlignment = xlCenter
Centres the text in the cells.
Columns("A:D").AutoFit
Adjusts the width of the columns so all the text fits.
Range("F1").Value = "Genererad: " & Date
Writes the text “Generated:” followed by today’s date in cell F1. & combines text, and Date is a VBA function that returns today’s date.
Note the comment: The line that starts with ' (apostrophe) is a comment. It isn’t run as code but is there to explain what the code does. Use comments liberally – your future self will thank you.
Run the new macro
Step 1: Click somewhere inside the macro “FormateraRubrik”
Step 2: Press F5
Step 3: Switch to Excel (Alt + F11 or click the Excel window)
Look at your worksheet. The header row is now formatted with a blue background and white, bold, centred text. The columns have been adjusted. Cell F1 shows today’s date.
This is automation. You can now run this macro on any report to get consistent formatting in seconds.
Understanding the VBA basics
Now that you have created two working macros, let’s understand the principles behind the code.
Objects, properties and methods
VBA is built on the object model. Excel consists of objects (things) that have properties (descriptions) and methods (actions).
Objects: – Application (Excel itself) – Workbook (an Excel file) – Worksheet (a worksheet) – Range (cells)
Properties: Describe the object – Font.Bold (is the text bold?) – Interior.Color (background colour) – Value (the value in a cell)
Methods: Actions you perform on the object – AutoFit (adjust size) – Copy (copy) – Delete (delete)
The structure is always: Object.Property = Value or Object.Method
Variables – Storing information
Variables are like boxes where you save information to use later.
Sub AnvändVariabler()
Dim namn As String
Dim ålder As Integer
namn = "Lukas"
ålder = 35
MsgBox namn & " är " & ålder & " år"
End Sub
Dim declares the variable (creates the box) As String/Integer specifies what type of data (text or whole number) Then we assign values and use them.
If-Then – Conditions
Sometimes you want to do different things depending on a condition:
Sub KontrolleraVärde()
Dim värde As Integer
värde = Range("A1").Value
If värde > 100 Then
MsgBox "Värdet är stort"
Else
MsgBox "Värdet är litet"
End If
End Sub
If the value in A1 is greater than 100 a message is shown, otherwise a different one.
Loops – Repeating actions
When you want to do the same thing many times you use loops:
Sub LoopExempel()
Dim i As Integer
For i = 1 To 10
Cells(i, 1).Value = "Rad " & i
Next i
End Sub
This writes “Row 1”, “Row 2”, etc. in cells A1 to A10.
For i = 1 To 10 means “count from 1 to 10” Cells(i, 1) means the cell in row i, column 1 (column A) Next i moves on to the next number
Make the macro easy to reach
Now you have working macros, but every time you want to run them you have to open the VBA editor and press F5. There are better ways.
Method 1: Add a button
Step 1: Go to the Developer tab
Step 2: Click Insert
Step 3: Under “Form Controls” select Button (the first icon)
Step 4: Click and drag to draw the button where you want it
Step 5: A dialogue box appears – select “FormateraRubrik” from the list
Step 6: Click OK
Step 7: Right-click the button and select “Edit Text” to rename it “Format”
Now you can click the button to run the macro.
Method 2: Keyboard shortcut
Step 1: Go to Developer > Macros
Step 2: Select “FormateraRubrik”
Step 3: Click Options
Step 4: In the “Shortcut key” field type a letter, e.g. “f”
Step 5: Click OK
Now you can press Ctrl + Shift + F to run the macro.
(It becomes Ctrl + Shift + [din bokstav] because Ctrl + [bokstav] is usually already used by Excel)
Method 3: Quick Access Toolbar
Step 1: Click the small down arrow in the Quick Access Toolbar (top left)
Step 2: Select “More Commands”
Step 3: In the dropdown under “Choose commands from” select “Macros”
Step 4: Select “FormateraRubrik” and click Add
Step 5: Click OK
Now there is a button for the macro in the toolbar, always available.
Save your workbook with macros
When you save a workbook with macros you must use the right file format.
IMPORTANT: Ordinary .xlsx files cannot contain macros. You must save as .xlsm (Excel Macro-Enabled Workbook).
Step 1: Click File > Save As
Step 2: Choose a location (e.g. this computer or OneDrive)
Step 3: In the “Save as type” dropdown, select “Excel Macro-Enabled Workbook (*.xlsm)”
Step 4: Give the file a name and click Save
The next time you open the file, Excel may warn you about macros. Click “Enable Content” to enable your macros.
For more on macro security, read our guide Excel macro security.
Troubleshooting – When something goes wrong
Everyone makes mistakes when writing code. Here is how to fix common problems.
“Compile error: Syntax error”
You have misspelled something or used the wrong syntax. Check: – Does every Sub have a matching End Sub? – Does every If have a matching End If? – Are all quotation marks closed correctly?
VBA often highlights the line containing the error in red.
“Run-time error ‘1004’”
This often means you are referring to a cell or range that doesn’t exist. Check: – Did you spell the cell reference correctly? – Does the sheet or cell you are referring to exist?
The macro does nothing
Check: – Is the cursor inside the right macro when you press F5? – Did you enable macros when you opened the file?
Debugging with MsgBox
When you don’t understand what is happening, use MsgBox to show values:
Sub DebugExample()
Dim värde As Integer
värde = Range("A1").Value
MsgBox "Värdet är: " & värde ' Visar vad variabeln innehåller
End Sub
Next steps on your VBA journey
You can now: – Open the VBA editor – Create modules and write macros – Format cells with code – Use variables, conditions and loops – Make macros easy to reach
What do you do next?
Extend your first macro
Experiment with the macro you have built. Try: – Changing the colours (try other RGB values) – Formatting more rows – Adding borders around the cells: Range("A1:D1").Borders.LineStyle = xlContinuous – Changing the font size: Range("A1:D1").Font.Size = 14
Learn more VBA commands
Check out our guide to the 10 most common Excel tasks you can automate for concrete examples of useful macros.
Build something meaningful
Think of a task you do often in Excel. Can parts of it be automated? Start simple: – Always format reports the same way? Build a formatting macro. – Often copy data from one file to another? Build an import macro. – Send the same report every week? Build an automated reporting system.
Take a course
Self-teaching takes you far, but structured training gives you the fundamentals faster. Our VBA course for beginners covers everything from the basics to advanced techniques in one day.
Read our main guide
For a deep dive into VBA and Excel automation, read our complete guide: Automate Excel with VBA.
Frequently asked questions for beginners
Do I need to know programming to learn VBA? No. VBA is designed for Excel users, not programmers. If you know Excel formulas you can learn VBA.
How long does it take to get good at VBA? You can build useful macros after a few hours. Becoming truly proficient takes a few months of regular use. The important thing is to start and build something useful right away.
Can I break something with VBA? Yes, a macro can delete data or overwrite cells. That is why you should always test on a copy first, and use “Undo” (Ctrl+Z) if something goes wrong. Professional macros have safety checks, but for beginners: start carefully.
Does VBA work on Mac? Yes, but some commands differ slightly. Most basic macros work identically on Windows and Mac.
Is VBA outdated? Should I learn Python instead? VBA is far from outdated. Millions of companies use it daily. Python is powerful for data analysis, but for Excel automation VBA is still the simplest and most integrated choice.
You’re up and running
15 minutes ago you knew nothing about VBA. Now you have written code, created macros and automated Excel tasks.
This is just the beginning. Every macro you build teaches you something new. Every automated task saves time and reduces errors. Every time you think “this is tedious to do manually” you now have the tool to automate it.
Welcome to the world of VBA. You will save hundreds of hours.
Are you stuck or want to learn more?
Book a free consultation and we’ll help you move forward. Or go straight to our VBA course for structured training.
Written by Lukas Lilja, Excel Department. We have helped hundreds of beginners take their first steps in VBA and build automation that saves time every day.
Need help with this? Our VBA course will help you – the first consultation is free of charge.
Book a free call or call 010-264 20 20.

