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 SubThis 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
Enter — commit · F2 — edit · F4 — $ in a reference · Ctrl+C/Ctrl+V — copy and paste · Ctrl+Z — undo
Esc or F6 — leave the sheet for the «Check» button · Shift+Tab in the first cell — back to the page