Skip to content
PremiumAdvanced

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

Start the course

Next: What macros do, and how to open them safely

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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

2. Write VBA

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

3. Dialogs, functions and events

  1. 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.

  2. 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.

  3. 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.

4. Debug and automate

  1. 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.

  2. 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.

5. Beyond VBA

  1. Premium

    Beyond VBA: Office Scripts, Python in Excel and Copilot

    Choose between VBA and Office Scripts for each job, move a weekly job to an Office Script for a team in the browser, and see where Python in Excel and Copilot fit.

ProExcel seal: =PROEXCEL() with a check, every formula checked

Certificate of completion

Finish all 15 lessons and solve all 62 exercises to earn it, then add it to LinkedIn.