Ravindra BagaleCourses & study guides मराठी Track your progress

12. Macros and VBA

12.18 Recording an Office Script and Reading the Code

Example first. Zoya does not want to write code yet. She wants Excel to watch her once: make the header row bold, fill it orange, and turn A1:I11 into a Table. The Action Recorder writes the script for her. Then she opens the code and finds that she can read most of it, because each recorded action becomes one line with a comment.

Recording an Office Script — in 30 seconds

What: The Action Recorder watches what you do in the workbook and writes an Office Script with one line per action. You can run it again later on the same or another workbook.

Why: It is the fastest way to start: no code to type, and the recorded code teaches you the method names you need when you later write scripts yourself.

Tiny example: After recording "bold the header", the script contains a line like getFont().setBold(true) for the range A1:I1.

First look at the data (before → after):

Recording an Office Script: before and after tables, fictional FreshGo Maharashtra data

Expected result — After running the recorded script

Range State
A1:I1 bold, orange fill
A1:I11 an Excel Table

About this data: Teaching sample for a fictional shop “FreshGo Mart”. Figures and names (e.g. Aarti Kulkarni) are made up for this lesson on recording an office script and reading the code.

Before table and formula as text

Before — Orders sheet before recording

Range State
A1:I1 plain headers
A1:I11 normal range, no Table

About this data: Demo table only — not real customer or company data. Built to practise recording an office script and reading the code.

Formula / click path

Automate › New Script › Create from Recording
do the steps … › Stop
Rename the script, then Edit to read the code

Do it step by step:

  1. Open the Orders workbook in Excel on the web (saved in OneDrive for Business).
  2. Select Automate › New Script › Create from Recording. The Record Actions pane opens and lists every action you take.
  3. Select A1:I1, press Ctrl + B, and choose an orange fill colour from Home › Fill Color.
  4. Select A1:I11 and press Ctrl + T, keep My table has headers ticked and select OK.
  5. Select Stop in the Record Actions pane. The new script appears. Select Rename and call it Format Orders.
  6. Select Edit to open the code in the Code Editor and read it with the table below.

Version note

Office Scripts need a work or school Microsoft 365 licence (Microsoft lists Office 365 Business, Business Premium, ProPlus, ProPlus for Devices, A3, A5, E1, E3, E5 and F3), the workbook stored in OneDrive for Business or SharePoint, and connected experiences turned on by your admin. They run in Excel on the web, Excel for Windows (Version 2210 or later) and Excel for Mac. Personal and Family plans can try them only as a preview through the Microsoft 365 Insider programme. If you do not see the Automate tab, your licence or your admin settings do not allow it yet.

What the recorded code looks like (illustrative: your recording may differ slightly, for example in the colour code or comment wording)

function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();
    // Set font bold to true for range A1:I1 on selectedSheet
    selectedSheet.getRange("A1:I1").getFormat().getFont().setBold(true);
    // Set fill color to #FFC000 for range A1:I1 on selectedSheet
    selectedSheet.getRange("A1:I1").getFormat().getFill().setColor("#FFC000");
    // Add a new table at range A1:I11 on selectedSheet
    let newTable = workbook.addTable(selectedSheet.getRange("A1:I11"), true);
}
Recorded line What it means
let selectedSheet = workbook.getActiveWorksheet(); Work on the sheet that is active when the script runs.
getRange("A1:I1").getFormat().getFont().setBold(true) Go to A1:I1 › its format › its font › make it bold.
getFormat().getFill().setColor("#FFC000") Fill colour as a hex code (#FFC000 is an orange).
workbook.addTable(selectedSheet.getRange("A1:I11"), true) Make a Table from A1:I11; true means the first row has headers.

About this data: Made-up rows so you can copy the pattern for recording an office script and reading the code. Currencies may use ₹, £, € or $.

Reading pattern. Almost every line follows "get something, then do something to it": getRange(…) › getFormat() › getFill() › setColor(…). Methods that start with get fetch an object or a value; methods that start with set change something.

Absolute vs relative recording (from Microsoft's recording page)

Mode What is recorded Best when
Absolute (default) The exact cell addresses: editing A3 edits A3 every time. Your data is always in the same place.
Relative Positions relative to the cell selected when you start: "two rows below the current cell". The script should act on the current cell or the cells around it.

About this data: Fictional worksheet used in class. Safe to edit; nothing here is live production data.

To switch, open the drop-down menu in the Record Actions pane and select Relative mode. (This is the same idea as Use Relative References for VBA macros in lesson 12.2.)

Things to fix after recording

  1. getActiveWorksheet() depends on which sheet is open. Replace it with workbook.getWorksheet("Orders") before you run the script from Power Automate; Microsoft's tutorial says flows should not use relative references like the active worksheet.
  2. Running the script twice adds a second Table on top of the first and fails. Recorded scripts do not check "is it already done?"; you add that logic in the Code Editor (lesson 12.19).
  3. Recorded scripts repeat the same getRange call on every line. Store it once in a variable, for example const header = selectedSheet.getRange("A1:I1");.

Microsoft sources: Record your actions as Office Scripts; Tutorial: automatically run scripts with Power Automate.

Practice task (try it before you open the answer):

  1. Record a script that sets the number format of G2:G11 to show two decimals and makes column G wider.
  2. Open it in the Code Editor and find the line that changes the number format.
  3. Change the script so it uses the sheet named Orders instead of the active sheet.
Answer
  1. Automate › New Script › Create from Recording, select G2:G11, apply Home › Number Format › Number, widen column G, then Stop and Rename.
  2. The recorded line uses a method like setNumberFormat(…) on the range G2:G11 (the exact format text depends on what you clicked).
  3. Replace let selectedSheet = workbook.getActiveWorksheet(); with let selectedSheet = workbook.getWorksheet("Orders");. Nothing else needs to change.

Ravindra Bagale's Tip

Record first, then read and tidy the code. Students who record ten small scripts learn the method names faster than students who start from a blank editor.

Worked example — recording an office script and reading the code

Why

Priya Deshmukh in Pune and Oliver Bennett abroad both need a clear, repeatable way to use recording an office script and reading the code on a Maple Bakery Co sheet without breaking totals.

What

Recording An Office Script And Reading The Code is the Excel idea this page teaches. We will walk one tiny example end to end.

How

  1. Open a blank sheet. In A1 type a short label for recording an office script and reading the code.
  2. Enter a small made-up value for Priya Deshmukh using € where money appears.
  3. Add a second value for Oliver Bennett using € so you see mixed currencies stay numeric.
  4. Apply the method from this lesson step by step (menu or formula as shown above).
  5. Check the result still works inside SUM or a simple check cell.

Formula / action

Follow the formula or ribbon steps shown in this lesson (keep values numeric).
Result: The demo cell shows the expected result for Maple Bakery Co's sample row.
Meaning: Once recording an office script and reading the code is correct on one row, copy the same pattern down the column.

Practice tasks (10+)

Try these on a blank sheet. Open a hint only if you are stuck. No quiz — just practice.

1. Rebuild the smallest example of recording an office script and reading the code on a blank sheet for Orbit Logistics.

Hint: Use only the steps on this page. Keep numbers numeric.

2. Change one input for Neha Shah and confirm the result updates.

Hint: If the result is stuck, check absolute vs relative references.

3. Repeat the same steps with a £ or € value for Anna Kowalski.

Hint: Formatting must not break SUM.

4. Write the result meaning in one plain sentence next to the cell.

Hint: Imagine explaining it to a new joiners.

5. Add a second row and fill the method down.

Hint: Double-click the fill handle if the pattern is consistent.

6. Break the example on purpose, then fix it using this lesson.

Hint: Common break: text instead of a number.

7. Apply recording an office script and reading the code to a 5-row made-up Orbit Logistics table.

Hint: Keep column headers clear.

8. Check your answer with a simple total or COUNT.

Hint: A wrong total usually means a format or key mismatch.

9. Swap ₹ and $ on one column and re-check totals.

Hint: Stored values stay the same; only the display changes.

10. List two mistakes beginners make with recording an office script and reading the code.

Hint: Look back at the tip box on this page.

11. Teach the steps out loud in under one minute.

Hint: Why → What → How.

12. Save the sheet as OrbitLogist_practice.xlsx.

Hint: Use a clear file name for class.

Loading practice tasks...

  1. Last used row (Easy): Which row number is the last filled row? The headings are in row 1.
  2. Late orders (Easy): How many rows have Status "Late"?
  3. Total amount (Easy): Add up the Amount column.
  4. Worst delay (Easy): What is the largest number in the Days late column?
  5. Late orders in Pune (Medium): How many orders are Late and in City "Pune"?
  6. Customer for an order (Medium): Find the Customer for Order ID "DL-704".
  7. Money stuck in late orders (Medium): Add up the Amount column for rows where Status is "Late".
  8. Big orders (Medium): How many rows have an Amount of 2000 or more?
  9. Message text (Medium): Build the text "N late orders", where N is the count of Late rows.
  10. Average delay of late orders (Hard): What is the average Days late for rows with Status "Late"? Round to 1 decimal.
  11. Tag for the first row (Hard): For the first row, return "Chase" if Days late is 3 or more, else "OK".
  12. Warning or all clear (Hard): Return "Warning" if any order is 3 or more days late, else "All clear".

Quick revision cards

Three standalone cards. Each one explains the idea with its own example. Scan the QR to open this lesson.

Revision card 1: What is Recording An Office Script And Reading The Code?
Card 1: What is Recording An Office Script And Reading The Code? · Download PNG
Revision card 2: Tiny example — Recording An Office Script And Reading The Code
Card 2: Tiny example — Recording An Office Script And Reading The Code · Download PNG
Revision card 3: Checklist — Recording An Office Script And Reading The Code
Card 3: Checklist — Recording An Office Script And Reading The Code · Download PNG