Macros and VBA: Automate Repetitive Work
Record a macro and run it from a list, a shortcut or a button, then read and write VBA that formats reports, loops over rows, makes decisions, asks before it acts, adds your own worksheet functions, reacts to changes and handles errors.
15 lessons · 62 exercises · 2 h 17 min
About this course
Some jobs come back every week with the same clicks: format the export, flag the late payments, add the totals, send it on. A macro does them for you. You start by recording one, running it from the Macros list or a shortcut key, and opening macro files from other people safely. Then you open the VB Editor and read what the recorder wrote.
From there you write VBA yourself: objects, properties and methods; Range, Cells, Offset and CurrentRegion; variables; If and Select Case; loops down to the last row; message and input boxes; functions you can use in a cell; code that runs when a cell changes; and debugging with error handling. A case study puts it together in a macro that builds a monthly report and runs from a button on the sheet. The last lesson places VBA next to Office Scripts, Python in Excel and Copilot.
VBA runs in Excel for Windows and Excel for Mac, and the lessons note where the Mac differs. It doesn't run in Excel for the web or on phones and tablets. In the exercises you complete and fix real macros in a code window, put steps in order and fill in Excel's dialogs, and every answer is checked at once.
Lessons
1. Record and run macros
- Not started
What macros do, and how to open them safely
Turn on the Developer tab, save a workbook so it keeps its macros, and open macro files from other people without letting harmful code run.
Free practice10 min - Premium
Record your first macro
Record a formatting routine with the macro recorder, run it from the Macros list or a shortcut key, and choose where Excel keeps it.
Premium10 min - Premium
Record a macro that works from any cell
Turn on Use Relative References so a recorded macro works from whichever cell is selected, and read the Offset lines it writes.
Premium9 min - Premium
Read and edit a macro in the VB Editor
Open the VB Editor, find a recorded macro in its module, read what each line does, and change a value or delete a line the macro doesn't need.
Premium9 min
2. Write VBA
- Premium
Read VBA as objects, properties and methods
Read a line of VBA as an object and what it has or does, call a method without an equals sign, and write to cells on any sheet without selecting them.
Premium9 min - Premium
Point at cells with Range, Cells, Offset and CurrentRegion
Refer to cells by address with Range, by row and column numbers with Cells, by distance with Offset, and to a whole block of data, however long, with CurrentRegion.
Premium9 min - Premium
Store values in variables, and shorten code with With
Declare variables with Dim and a type, let Option Explicit catch misspelled names, give a range to a variable with Set, and group the lines that work on one object with With.
Premium9 min - Premium
Make decisions with If and Select Case
Make a macro act on a cell's value with If, ElseIf and Else, avoid the boundary and capital-letter traps, and choose between several bands with Select Case.
Premium9 min - Premium
Repeat steps for every row with For and For Each
Run the same lines for every row with For...Next down to the last filled row, walk the cells of a range with For Each, and delete rows from the bottom up.
Premium9 min
3. Dialogs, functions and events
- Premium
Ask and confirm with MsgBox and InputBox
Tell people what a macro did with MsgBox, ask before a step that can't be undone with a Yes and No box, and read a value with InputBox, including what happens when someone selects Cancel.
Premium9 min - Premium
Write your own worksheet function
Write a Function procedure that works in a cell like a built-in function, give it arguments, return its result through its name, and know what a function in a cell isn't allowed to do.
Premium9 min - Premium
Run code when a cell changes or a workbook opens
Write an event procedure that runs by itself when a cell changes, in the right module and under the right name, react only to the cells you care about, and turn events off while it writes.
Premium9 min
4. Debug and automate
- Premium
Find a macro's bugs and handle its errors
Find the line a run-time error stops on, know what stepping through a macro does, and catch errors with On Error so the macro stops with a clear message.
Premium9 min - Premium
Case study: automate a monthly sales report
Put the course together in one macro that turns a raw export into a formatted report with flags and a total row, asks before it runs, and sits on a button on the sheet.
Premium11 min
5. Beyond VBA
Certificate of completion
Finish all 15 lessons and solve all 62 exercises to earn it, then add it to LinkedIn.