🧩 Excel automation examples

Office Scripts Find and Replace Examples

Find and replace values across ranges, sheets, and tables in Office Scripts. Use these examples as practical patterns, then adjust worksheet names, table names and ranges for your own workbook.

Updated 2026-06-125 practical examplesCopy-ready code

Syntax or pattern

This is the basic pattern for this type of automation. Copy the code, then replace workbook-specific names before running it.

function main(workbook: ExcelScript.Workbook) { ... }
✍️

5 practical examples

1

Basic find and replace script

Use this pattern as a starting point for find and replace values across ranges, sheets, and tables in office scripts.

function main(workbook: ExcelScript.Workbook) { const sheet = workbook.getActiveWorksheet(); const usedRange = sheet.getUsedRange(); if (!usedRange) return; console.log(`Rows: ${usedRange.getRowCount()}`); }

Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.

2

Target a specific worksheet

Run the script against a named worksheet instead of the active sheet.

function main(workbook: ExcelScript.Workbook) { const sheet = workbook.getWorksheet('Report'); if (!sheet) return; sheet.getRange('A1').setValue('Updated by Office Script'); }

Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.

3

Work with an Excel table

Read or update a structured table in the workbook.

function main(workbook: ExcelScript.Workbook) { const table = workbook.getTable('SalesTable'); const rows = table.getRangeBetweenHeaderAndTotal().getValues(); console.log(rows.length); }

Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.

4

Add a status timestamp

Write a simple timestamp so users know the automation has run.

function main(workbook: ExcelScript.Workbook) { const sheet = workbook.getActiveWorksheet(); sheet.getRange('A1').setValue('Last updated'); sheet.getRange('B1').setValue(new Date().toLocaleString()); }

Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.

5

Simple safety check

Stop the script when the expected worksheet or table is missing.

function main(workbook: ExcelScript.Workbook) { const sheet = workbook.getWorksheet('Data'); if (!sheet) { throw new Error('Data sheet was not found.'); } }

Adjust worksheet names, table names and ranges to match your own workbook before using this pattern.

Safety notes before you run automation

  • Test the code on a copy of the workbook first.
  • Save important files before running code that deletes, moves, formats or emails data.
  • Check worksheet names, table names and range references before copying the example.

Common mistakes to avoid

  • Running automation on the only copy of a workbook instead of a backup.
  • Assuming the active worksheet is always the sheet you meant to update.
  • Forgetting that macros and scripts can change many cells very quickly.

FAQ

Is this Office Script safe to copy?

Use the code as a learning pattern. Test it on a copy of your workbook before running it on important files.

Why does the code not work in my workbook?

Usually the sheet, table, range, or column name is different. Update names and test on a small sample first.

Should I use VBA or Office Scripts?

Use VBA for desktop Excel macros and legacy workbooks. Use Office Scripts for Excel on the web and cloud automation workflows.

💡 Useful resources

Here are some ideas for you

Optional resources that may help if you are learning Excel automation, building reports, or automating spreadsheets often.

  • 📘
    Excel VBA books

    Learn macro patterns, objects, ranges and real automation examples.

    See ideas
  • 🧩
    Office Scripts resources

    Practice automating Excel on the web with repeatable script patterns.

    See ideas
  • ⌨️
    Productivity keyboards

    Helpful for writing code, formulas and documentation for long sessions.

    See ideas
  • 🖥️
    External monitors

    View the workbook, VBA editor, script editor and notes side by side.

    See ideas
  • 💾
    External backup drives

    Useful for keeping backup copies of automation workbooks and reports.

    See ideas
  • 📒
    Workflow notebooks

    Sketch macro steps, report logic and safety checks before coding.

    See ideas

Some links in this section may be affiliate links. Choose only what is useful for your own work.