VBA & Macro Problems

How to Use an Excel Office Script on Multiple Sheets with Variable Rows

Amos GikundaAmos Gikunda Nov 11, 2026 869 views

Question details

The user needs to adapt an existing single-sheet Office Script so that it can dynamically process multiple worksheets and automatically handle a variable number of data rows on each sheet.

How to Use an Excel Office Script on Multiple Sheets with Variable Rows
Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating data preparation, such as inserting columns, filling values, and creating tables across an entire workbook with sheets of different lengths.
Observed behavior
The current script only runs on one specific worksheet and relies on a hardcoded range, failing to adapt when other worksheets have more or fewer data rows.
Before you start

Before modifying and testing loop-based scripts, create a backup copy of your workbook to prevent accidental data overwrites or incorrect table formatting on unintended worksheets.

Solution 1Recommended

Modify the Office Script to Loop Worksheets and Calculate Used Ranges

Iterate through all worksheets using a loop and dynamically calculate the used range for each sheet before resizing and applying table formatting.

When automating tasks across an entire workbook, hardcoding sheet names and cell ranges limits your script's flexibility. By utilizing the workbook's sheet collection and calculating the used range dynamically, your script can safely adjust to sheets of varying data lengths.

1
Loop through workbook worksheets

Begin by wrapping your main logic in a loop using `for (let sheet of workbook.getWorksheets())`. This ensures the script iterates over every sheet in the current file.

2
Filter irrelevant or blank worksheets

Add an `if` statement at the start of your loop to exclude blank sheets or those lacking the expected layout, preventing errors during table creation.

3
Calculate the dynamic row count

Identify the data boundaries by calling `sheet.getRange("B3:B10000").getUsedRange()` (adjusting the column as needed for your layout). Then, use `getRowCount()` on the returned range to capture the exact number of variable data rows.

4
Resize the fill and table ranges

Use the `getResizedRange()` method based on your calculated row count to accurately map the destination area. Create the table starting from the header row through the last detected data row.

Modify the Office Script to Loop Worksheets and Calculate Used Ranges
Validating Ranges: Always verify that the range returned by getUsedRange() is valid before creating your table. If a user accidentally formatted the entire column, it could create an excessively large table and slow down performance.
Free Microsoft Office alternative

Automate Workbooks Easily with WPS Office

If you frequently work with complex macros and scripts but want a more cost-effective solution, WPS Office is an excellent free alternative. It offers native support for both VBA and modern JS Macros, ensuring seamless compatibility with your existing spreadsheet automation tasks.

  1. 1. Download and Install: Download WPS Office for free from the official website and install it on your device.
  2. 2. Open your Spreadsheet: Launch WPS Spreadsheets and open your existing .xlsx or .xlsm file containing your data.
  3. 3. Access the Developer Tools: Navigate to the 'Tools' or 'Developer' tab to access the built-in Macro Editor for automating your cross-sheet tasks.
Fully compatible with Microsoft Excel (.xlsx, .xlsm, .xls) file formats.Built-in support for VBA and JS Macros to handle complex workbook automation.Lightweight architecture ensuring fast execution of large datasets.Free to use with a familiar, easy-to-navigate interface.
microsoft office alternative - wps office

Frequently Asked Questions

How do I exclude specific sheets from the Office Script loop?

You can use an `if` statement to check the sheet's name with `sheet.getName()`. If the name matches sheets you want to skip, such as 'Summary' or 'Dashboard', execute a `continue;` statement to skip to the next iteration.

Why does getUsedRange() sometimes include empty rows?

In spreadsheet applications, cells that have been formatted (e.g., background color, borders) but lack data are still considered 'used'. To fix this, you can refine your script to search for the last non-empty cell in a specific column instead.

Can I run this dynamic script on workbooks with completely different structures?

It is not recommended unless your script includes strict validation checks. If a sheet does not have the expected headers or column layout, blindly inserting columns and creating tables may corrupt the existing data structure.