01The requirement: let users automate their own documents
| Requirement | Why | What macros offered |
|---|---|---|
| Repeat keystroke sequences | Monthly reports needed the same 50 commands every time | Recorded keystroke macros |
| Custom functions and forms | Each office has its own calculations and data entry | User-defined functions, dialog boxes and buttons |
| Link applications | Data moves from Excel to Word letters and Outlook e-mails | One macro language across the Office suite (VBA) |
| No separate programming tools | Users had Office, not compilers | An editor built into the application |
| Share the automation | The tool should travel with the document | Macros stored inside the file — which also let viruses travel |
02From keystrokes to VBA
\A. Later versions added {IF}, {BRANCH} and {GETNUMBER}, making it a small programming language. Whole accounting systems were built this way.
\A {GOTO}A1~/WCS12~{DOWN}/RFC2~D2..D20~
Go to A1, set column width 12, move down, format D2:D20 as fixed with 2 decimals.=SELECT("R1C1"), =FORMAT.FONT(,,TRUE). Excel still recognises them; attackers revived them around 2020, and Microsoft restricted them by default in January 2022.03Try it: record a macro
Excel's macro recorder (View → Macros → Record) writes code for whatever you do. Click the actions below on the test log; the "recorder" writes the equivalent VBA, Google Apps Script or Excel Office Script.
The real recorder writes clumsier code (lots of Select and Selection); good macros, like these, act on ranges directly.
04Macro viruses and how Office fought back
Because macros live inside documents and can run automatically when a file opens (AutoOpen, Workbook_Open), they became an ideal virus carrier.
| Year | Event |
|---|---|
| 1995 | Concept, the first widespread Word macro virus, spread through shared documents worldwide within months. |
| 1999 | Melissa used Word VBA to mail itself to the first 50 Outlook contacts (Episode 32). |
| 1997–2007 | Macro security levels, digital signatures and, with Office 2007, separate .docm/.xlsm formats for files with macros; plain .docx/.xlsx cannot contain them. |
| 2010 | Protected View opens downloaded files read-only; the yellow "Enable Content" bar appears. |
| 2014–2021 | Banking trojans such as Emotet persuaded users to click "Enable Content" in fake invoices. |
| 2022 | Microsoft restricted Excel 4.0 macros in January, then made Office block VBA macros in files carrying the "Mark of the Web" (downloaded or e-mailed) — with no enable button — for Word, Excel, PowerPoint, Access and Visio, from version 2203. |
.xlsx for files you send to others.05An Indian macro language: Tally's TDL
Tally (Episode 31) took a different route. The product itself is described in Tally Definition Language (TDL), a non-procedural language of definitions — menus, reports, forms, parts, lines and fields — plus actions and functions. Tally partners write TDL files to add fields, reports, invoice formats, workflows and integrations; TallyPrime loads them at start-up without changing the core program. TallyPrime Developer is the official editor and compiler, and Tally lists hundreds of ready-made extensions.
The traditional first TDL program adds a menu item that opens a report showing one line of text. It shows the structure every TDL report follows: Report → Form → Part → Line → Field.
;; Hello.tdl — adds "Hello Report" to the Gateway of Tally menu
[#Menu: Gateway of Tally]
Add : Item : Hello Report : Display : Hello Report
[Report: Hello Report]
Form : Hello Form
[Form: Hello Form]
Parts : Hello Part
[Part: Hello Part]
Lines : Hello Line
[Line: Hello Line]
Fields : Hello Field
[Field: Hello Field]
Set As : "Hello from TDL - Test Lab accounts"
Width : 60Load it from F1: Help → TDL & Add-On → Manage Local TDLs in TallyPrime (menu names vary by release). Learn TDL on a test company, not your live books; Tally offers a free "TDL Essential" course.
06Ready-to-use macros
Paste VBA into the Excel editor (Alt+F11 → Insert → Module), save as .xlsm, and run with Alt+F8. Try them on copies of your files.
A. Merge every CSV in a folder into one sheet (Excel VBA)
Sub MergeCsvFiles()
' Import every CSV in a folder into a new sheet; column A records the file name.
Dim folder As String, f As String
Dim ws As Worksheet, src As Range, nextRow As Long, n As Long
folder = "D:\Archive\Data\" ' must end with a backslash
Set ws = ThisWorkbook.Worksheets.Add
nextRow = 1
Application.ScreenUpdating = False
f = Dir(folder & "*.csv")
Do While f <> ""
Workbooks.Open folder & f
Set src = ActiveWorkbook.Worksheets(1).UsedRange
If nextRow > 1 Then ' keep the header only once
If src.Rows.Count < 2 Then GoTo NextFile
Set src = src.Offset(1).Resize(src.Rows.Count - 1)
End If
n = src.Rows.Count
src.Copy ws.Cells(nextRow, 2)
ws.Cells(nextRow, 1).Resize(n).Value = f
nextRow = nextRow + n
NextFile:
ActiveWorkbook.Close SaveChanges:=False
f = Dir()
Loop
Application.ScreenUpdating = True
MsgBox (nextRow - 1) & " rows merged into sheet " & ws.Name
End SubB. Save every sheet as its own PDF (Excel VBA)
Sub ExportSheetsToPdf()
' Writes one PDF per worksheet next to the workbook (save the workbook first).
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "\" & ws.Name & ".pdf", _
Quality:=xlQualityStandard, OpenAfterPublish:=False
Next ws
MsgBox "PDFs saved in " & ThisWorkbook.Path
End SubC. E-mail a daily summary from a Google Sheet (Apps Script)
// Extensions → Apps Script; then add a daily time-driven trigger.
function emailDailySummary() {
const sheet = SpreadsheetApp.getActive().getSheetByName('Log');
const rows = sheet.getDataRange().getValues().slice(1); // skip header
const temps = rows.map(r => Number(r[3])).filter(v => !isNaN(v)); // column D
const body = 'Runs logged: ' + rows.length +
'\nMaximum oil temperature: ' + Math.max(...temps) + ' °C';
MailApp.sendEmail(Session.getActiveUser().getEmail(), 'Daily test summary', body);
}D. Bold the header row (LibreOffice Basic)
Sub BoldHeader
' Tools → Macros → Edit Macros; works in LibreOffice Calc.
Dim sheet As Object
sheet = ThisComponent.Sheets.getByIndex(0)
sheet.getCellRangeByName("A1:D1").CharWeight = com.sun.star.awt.FontWeight.BOLD
End Sub07Today's choices
| Tool | Language | Runs where | Best for |
|---|---|---|---|
| VBA | Visual Basic | Desktop Office (Windows, Mac) | Existing workbooks, Office-to-Office automation; no longer developed but fully supported |
| Office Scripts | TypeScript | Excel on the web and desktop (Microsoft 365) | Cloud workbooks; can be run by Power Automate (Episode 39) |
| Apps Script | JavaScript | Google's cloud | Sheets, Forms, Gmail and Drive automation |
| LibreOffice Basic / Python | StarBasic, Python | LibreOffice desktop | Open-source office automation (Episode 18) |
| Python (openpyxl, pandas) | Python | Anywhere, outside Excel | Generating and analysing many workbooks without opening Excel |
| Tally TDL | TDL | TallyPrime | Accounting customisation and integration in Indian businesses |
08Version explorer
Blue chips: desktop macro languages. Amber chips: cloud and business scripting.
09Lessons for engineers
- Record, then rewrite. The recorder shows the object names; clean code acts on ranges without selecting them.
- Keep macros out of shared files. Put tools in a personal macro workbook or an add-in; send colleagues plain
.xlsx. - Document the macro in the workbook. A sheet named "README" with purpose, author and version saves the next user.
- Know when to leave Excel. When a macro grows past a few hundred lines or handles millions of rows, move to Python or a database (Episode 05).