FormulaCamp
Completed
13. VBA and Macros · Introduction · 20 min

1. VBA: Your First Macros

Lesson 1 of 7
    Lesson theory

    VBA (Visual Basic for Applications) is a programming language built into desktop Excel. A macro is a procedure that performs actions for you.

    • Turn on the Developer tab. Record Macro turns your clicks into code, which is a good way to learn;
    • open the editor with Alt+F11;
    • save a workbook with macros as .xlsm;
    • VBA doesn't work in Excel for the web. There, automation means Office Scripts.
    Sub Totals()
        Dim cell As Range
        For Each cell In Range("B2:B10")
            If cell.Value > 100000 Then cell.Interior.Color = vbYellow
        Next cell
        Range("B11").Formula = "=SUM(B2:B10)"
    End Sub

    This macro fills cells greater than 100,000 with yellow and writes a sum formula. Note that in VBA, formulas are written in English with commas (.Formula). For formulas in the user's local language there is .FormulaLocal.

    Security: a macro is a program. Don't enable macros in files from unknown sources.

    Test yourself

    1. How do you open the VBA editor?
    2. In which format should you save a workbook with a macro?
    3. What does the line For Each cell In Range("B2:B10") do?
    4. Where does VBA not work?