VBA & Macro Problems

How to Efficiently Loop and Fill Multiple Excel Columns with Office Scripts

Huma Ashraf ChHuma Ashraf Ch Nov 11, 2026 868 views

Question details

The user is looking for an efficient way to automate the process of filling data across multiple spreadsheet columns (e.g., A through D) using Office Scripts, specifically seeking loop-based methods to avoid repetitive copy operations.

How to Efficiently Loop and Fill Multiple Excel Columns with Office Scripts
Product
Excel
Device & OS
not provided
Scenario
Automating spreadsheet data entry and extending values or formulas down multiple specific columns simultaneously.
Observed behavior
The user requires an optimized Office Script routine that uses range-based iteration or an array of cell addresses to cleanly copy and resize data ranges.
Before you start

Ensure your Excel workbook is saved to SharePoint or OneDrive, and verify that your Microsoft 365 account has the correct permissions to create and run Office Scripts.

Solution 1Recommended

Use an Array Loop to Resize and Copy Ranges

Define specific cell addresses in an array and use a loop to iterate through them, systematically copying the values down to the end of your used range.

This method is highly efficient because it isolates the specific columns you want to target without hardcoding repetitive operations for each individual column.

1
Open the Code Editor

Navigate to the 'Automate' tab on the Excel ribbon and click 'New Script' to open the Office Scripts editor.

2
Define the Worksheet and Row Count

Within your main function, declare the active worksheet using `workbook.getActiveWorksheet()`. Calculate the total rows to fill by taking `getUsedRange().getRowCount()` and subtracting your header rows.

3
Create the Array and Loop

Declare an array of your target starting cells, such as `const cells = ["A2", "B2", "C2", "D2"];`. Set up a `for (const cell of cells)` loop to iterate through them.

4
Execute the Copy Operation

Inside the loop, use `selectedSheet.getRange(cell).getResizedRange(fillRows, 0).copyFrom(cell, ExcelScript.RangeCopyType.values);` to populate the columns dynamically.

Use an Array Loop to Resize and Copy Ranges
Performance Boost: Looping through a concise array prevents repetitive manual code blocks, making your script run faster and significantly easier to maintain.
Automate with WPS Office

Automate Column Filling Offline with WPS JS Macros

WPS Office features a powerful built-in JavaScript Macro editor (JSA) that allows you to automate repetitive tasks like filling multiple columns. It works completely offline and is fully compatible with classic Excel file formats.

  1. 1. Access the JS Macro Editor: Open your workbook in WPS Spreadsheet, navigate to the 'Tools' tab, and click on 'JS Macro'.
  2. 2. Create a New Module: In the WPS Macro Editor, insert a new module to begin writing your JavaScript automation.
  3. 3. Write the Loop Array: Define your target columns in an array, such as `let cols = ["A", "B", "C", "D"];`, and set up a standard `for` loop.
  4. 4. Apply the AutoFill Method: Within the loop, utilize the `Range.AutoFill` method to dynamically populate the rows beneath your header.
  5. 5. Run Your Script: Save your code and click the 'Run' button to instantly execute the script and fill your spreadsheet columns.
Free and lightweight Office suiteFully compatible with Microsoft Excel formats (.xlsx, .xlsm)Built-in offline JavaScript (JSA) and VBA macro supportNo cloud subscription required to run automation scripts
microsoft office alternative - wps office

Frequently Asked Questions

Why should I use a loop instead of copying columns individually in Office Scripts?

Using a loop reduces code duplication and streamlines execution. It makes your script significantly shorter and much easier to update if you need to add, modify, or remove target columns in the future.

How do I dynamically find the last row to fill in Office Scripts?

You can calculate the total number of populated rows by calling `selectedSheet.getUsedRange().getRowCount()`. Subtracting the number of header rows from this total gives you the exact parameter needed for the `getResizedRange` method.

Does getResizedRange copy formulas or just plain values?

By default, the `copyFrom` method's behavior depends on the parameters you set. If you specify `ExcelScript.RangeCopyType.values`, it will only paste plain values. To copy formulas, you should use `ExcelScript.RangeCopyType.formulas` or omit the specific type parameter to copy everything.

Does WPS Office support Microsoft Office Scripts?

WPS Office does not natively run Microsoft Office Scripts, which are cloud-based and rely on TypeScript for Microsoft 365. However, WPS provides a highly capable offline JavaScript Macro (JSA) environment and standard VBA support to accomplish identical automation tasks.