OfficeScripts to loop through rows

Here’s an example of an OfficeScript that loops through rows in a specific column until a cell value no longer exists:

function main(worksheet: ExcelScript.Worksheet, columnLetter: string) {
  // Define the starting row
  let currentRow = 1;

  // Loop through rows until the cell value exists
  while (worksheet.getCell(currentRow, columnLetter).getValue()) {
    // Get the cell value
    let cellValue = worksheet.getCell(currentRow, columnLetter).getValue();

    // Do something with the cell value
    console.log(`Row ${currentRow}, ${columnLetter}: ${cellValue}`);

    // Increment the row number
    currentRow++;
  }
}

To use this OfficeScript, follow these steps:

  1. Open Excel Online or Excel for the web.
  2. Open an existing workbook or create a new workbook.
  3. Click on the “Automate” tab in the ribbon.
  4. Click on the “Script Lab” button.
  5. In the Script Lab pane, create a new script or open an existing one.
  6. Replace the default code in the script editor with the code provided above.
  7. In the main function call, provide the column letter as an argument (e.g., “A” for column A).
  8. Click the “Run” button to execute the script.

This script starts at the first row and continues looping through the rows until the cell value in the specified column no longer exists. Within the loop, you can perform any desired actions or operations with the cell value. In this example, it logs the row number, column letter, and cell value to the console using console.log(). The row number increments with each iteration.

Please note that OfficeScripts are currently only supported in Excel for the web and Excel Online. They are not available in the desktop version of Excel.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2025 Pamai Tech