VBA practice exercises, sorted by level and topic
118 exercises across 41 lessons, from your first macro to Dictionaries and events. You write every macro yourself, run it in the browser and see straight away what it changed on the sheet.
In short: every VBA exercise is a small office job on a worksheet, such as filling in an invoice, working down a list or creating one sheet per region. You write the macro, run it as often as you like and have it checked. No Excel install needed, with hints and a worked solution.
Start right now: three warm-up exercises
The first copies a value to another cell, the second swaps two values using a variable, the third adds up a column with a loop. All three are free. If you solve them without a hint, feel free to jump to a later topic in the list below.
Task Write a macro CopyValue that copies the contents of A2 to B2. Check also tests it with different text in A2.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Original | Copy | ||
| 2 | Invoice 1043 | |||
| 3 | ||||
| 4 | ||||
| 5 |
Task The import mixed up first and last name: A2 holds the last name, B2 the first name. Swap the two values. To do that, keep one value in a variable that you declare with Dim … As String.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | First name | Last name | ||
| 2 | Morgan | Emily | e.morgan@example.com | |
| 3 | ||||
| 4 | ||||
| 5 |
Task Write a macro LineTotals that fills column D with quantity times unit price for rows 2 to 8. Use a For Next loop with the counter i. Check also tests it with other quantities.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Item | Qty | Unit price | Total |
| 2 | Copy paper A4 | 10 | 4.95 | |
| 3 | Lever arch file | 25 | 2.49 | |
| 4 | Ballpoint pens, blue | 50 | 0.89 | |
| 5 | Sticky notes | 12 | 1.79 | |
| 6 | Black toner | 2 | 64.90 | |
| 7 | Clear pockets | 5 | 3.20 | |
| 8 | Staples | 8 | 1.15 |
How the VBA exercises work
At the top you see the worksheet your macro works on, below it the editor with a template. Run (F5 or Ctrl+Enter) executes your macro and shows the sheet afterwards, with changed cells highlighted. MsgBox pops up as a message box, Debug.Print goes to the Immediate window. Step through (F8) runs the macro one line at a time while you watch your variables. Every run starts from the original sheet, so you cannot break anything.
Check compares the sheet after your macro with the sheet after the model solution: every cell, plus the formatting in formatting tasks and the MsgBox text where a message is part of the task. How you write the macro is up to you. For Next or For Each, Range or Cells, with or without a helper variable: if the sheet is right at the end, the task is solved. Only when an exercise is about a particular command, such as Select Case or End(xlUp), does it have to appear in your code, and some exercises rule out .Select.
Checked against different data
Once your result is right, your macro runs again on a sheet you cannot see: different values, usually more rows. The model solution runs on the same data. A loop that stops at a fixed row 10 instead of finding the last row, or a result typed in instead of calculated, gets caught here, and the message tells you your macro only works for the data you can see.
Messages, hints and solutions
If your macro hits an error, you get the same wording as in the VBA editor, such as "Run-time error '13': Type mismatch" or "Compile error: Next without For", with the line highlighted and a note on the usual cause. If the result is wrong, the check names the first cell that differs from what was expected.
Stuck? There are three hints, each giving away a little more: first the idea, then the structure, then an almost finished line. The model solution and its explanation only appear when you ask for them, and some exercises also show other valid ways to solve them.
Points
You earn points the first time you solve an exercise, more for later levels and projects. With a hint you get half, after viewing the solution none. With a free account your points, solved exercises and streak are saved on every device.
All VBA exercises by level and topic
The lessons follow the learning path of the VBA course. Each page explains its topic with examples and has two or three exercises, the first one the easiest. Every level ends with a three-part project.
Level 1: First macros
- Basics: editor, module, first macro: 3 exercises
- Record a macro and clean it up: 2 exercises
- Range and Cells: 3 exercises
- Variables and data types: 3 exercises
- MsgBox and InputBox: 3 exercises
- Calculations and joining text: 3 exercises
- If Then Else: 3 exercises
- Project: fill in an invoice: 3 exercises
Level 2: Loops and ranges
- For Next loop: 3 exercises
- Find the last row: 3 exercises
- For Each: 3 exercises
- Do While and Do Until: 3 exercises
- Select Case: 3 exercises
- Offset and Resize: 3 exercises
- Project: commission list: 3 exercises
Level 3: Sheets, formats and functions
- Working with worksheets: 3 exercises
- Formatting cells: 3 exercises
- Worksheet functions in VBA: 3 exercises
- String functions: 3 exercises
- Dates and times: 3 exercises
- User-defined functions: 3 exercises
- Project: monthly report: 3 exercises
Level 4: Processing data
- Copy and paste: 3 exercises
- Delete and insert rows: 3 exercises
- Sort and remove duplicates: 3 exercises
- Searching with Find: 3 exercises
- Writing formulas with VBA: 3 exercises
- With and faster macros: 3 exercises
- Project: clean up data: 3 exercises
Level 5: Advanced
- Arrays: 3 exercises
- Dictionary and Collection: 3 exercises
- Error handling: 3 exercises
- Procedures with arguments: 3 exercises
- Debugging: 3 exercises
- Events (Worksheet_Change): 2 exercises
- Project: customer report: 3 exercises
Level 6: Real work and final test
- Combine sheets: 3 exercises
- Create a report with a macro: 3 exercises
- Enable macros and save safely: 3 exercises
- Final project: 3 exercises
- VBA final test
On top of that, the page VBA interview questions has tasks in interview style, including several find-the-bug exercises.
How to get the most out of the exercises
- Run before you check. Look at the sheet after your macro before you press Check. You will spot many mistakes yourself: one row short, a value in the wrong column.
- Use Step through. If you cannot tell what your macro is doing, run it line by line with F8 and watch the variables. That is how you find most loop bugs.
- Start small. Write one line that works for row 2. Once it does, wrap the loop around it.
- Read the task closely. It names the target cells, the macro name and, for messages, the exact text. A missing full stop in a MsgBox is enough for the check to differ.
- Try without a hint first. Two or three failed attempts are normal. Ask for a hint when you are really stuck.
- Come back later. With an account, "Review today" brings back exercises you solved a while ago. New tasks with other values are in the random VBA exercises.
Exercises with solutions, built for doing
Every task has a model solution, but it is not printed next to the task; it comes from the server when you ask for it. If you read the answer first, you usually just copy it and have forgotten it a week later. If you try it yourself and read the error messages along the way, it sticks. If you did look at a solution, solve the exercise again without help a few days later.
Ready-made macros to read through, sorted by common jobs such as "delete blank rows" or "one sheet per category", are on VBA macro examples. To experiment freely without a task, use the VBA online editor.
FAQ
Does every VBA exercise have a solution?
Yes. Each task has a model solution and an explanation of why it works, and three hints before that. Your macro can look different from the model solution as long as the sheet is right at the end.
Are the VBA exercises free?
Partly. All exercises in level 1, including its project, are free, and so is the first exercise of every lesson from level 2 on. The other exercises and the projects from level 2 on are part of Premium. You can read every lesson for free.
Do I need Excel for the exercises?
No. The macros run on our server in a VBA engine of our own with a worksheet. All you need is a browser, on a Mac, tablet or phone too. Code you write here runs the same way in real Excel, as long as it uses the commands the course covers.
What can the engine not do?
Anything that needs files or other programs: opening and saving workbooks, Outlook, Shell. Also UserForms, charts, PivotTables, AutoFilter and class modules. The exercise tells you these are not available in the exercises. None of the course tasks need them.
Is my progress saved?
With a free account, yes. The learning path then shows a tick next to every lesson whose exercises you have solved. Your browser also remembers code you have started, even without an account.