VBA & Macro Problems

How to Sort an Entire Used Range with Office Scripts in Excel

Bushra ParveenBushra Parveen Nov 10, 2026 868 views

Question details

The user needs an Office Script to sort a complete dataset by a specific header without disconnecting row data or rearranging columns.

How to Sort an Entire Used Range with Office Scripts in Excel
Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating the organization of spreadsheet data using Office Scripts.
Observed behavior
The current Office Script either sorts only a single column or unexpectedly rearranges the column order instead of sorting full rows.
Before you start

Ensure your Excel worksheet contains a clear header row without merged cells, and that Office Scripts are enabled for your Microsoft 365 account.

Solution 1Recommended

Apply Row-Oriented Sorting on the Used Range

Use the getUsedRange() method and explicitly set the sort orientation to rows to keep records intact.

When an Office Script unexpectedly rearranges columns instead of sorting data rows, the sort orientation parameter is likely defaulting or set to columns. By fetching the worksheet's entire used range and explicitly setting the orientation to ExcelScript.SortOrientation.rows, you can ensure that complete records are sorted together based on your target column.

1
Define the target range

Retrieve the full dataset dynamically by adding this line to your script: const range = workbook.getActiveWorksheet().getUsedRange();

2
Clean up range formatting (Optional)

To prevent formatting issues, reset alignments by extracting the format with const format = range.getFormat(); and applying methods like format.setWrapText(false); and format.setIndentLevel(0);

3
Identify your sort key

Locate the column index you wish to sort by (for example, the 'Amount' column) and assign it to a variable, such as sortedColumnIndex.

4
Execute the row-based sort

Apply the sort to your range using the following syntax: range.getSort().apply([{ascending:true, key:sortedColumnIndex}], false, true, ExcelScript.SortOrientation.rows);. The SortOrientation.rows parameter ensures records are sorted properly.

Apply Row-Oriented Sorting on the Used Range
Data Integrity Preserved: Using SortOrientation.rows ensures that all cells in a row move together, preventing data mismatch across your spreadsheet.
Free Microsoft Office alternative

Need a Lightweight Alternative for Spreadsheet Automation?

If you are struggling with complex Microsoft 365 subscriptions or Office Scripts limitations, WPS Office offers a powerful, free alternative. WPS Spreadsheet provides full compatibility with Excel files and includes a built-in JS Macro Editor, giving you a seamless JavaScript automation experience without the premium price tag.

  1. 1. Download and Install: Download WPS Office for free from the official website and install it on your computer.
  2. 2. Open Your Workbook: Launch WPS Spreadsheet and open your existing Microsoft Excel (.xlsx) file to preserve your data.
  3. 3. Explore JS Macros: Navigate to the Tools or Developer tab to open the WPS JS Macro editor and begin writing JavaScript automation scripts.
100% compatible with Microsoft Excel (.xlsx, .xls, .csv) formatsBuilt-in JS Macro Editor for powerful, lightweight spreadsheet automationFree to use with a familiar, tabbed user interfaceSeamless migration of your existing data and formulas
microsoft office alternative - wps office

Frequently Asked Questions

Why is my Office Script rearranging columns instead of sorting rows?

This happens when the sort orientation parameter is set to columns (ExcelScript.SortOrientation.columns). To fix this, you must explicitly pass ExcelScript.SortOrientation.rows in your getSort().apply() method. This forces the script to use column values as the key while keeping whole rows together.

How do I dynamically select all data in an Office Script?

You can dynamically capture all populated data on a sheet by calling the getUsedRange() method on your active worksheet, like so: workbook.getActiveWorksheet().getUsedRange();. This automatically detects the outer boundaries of your data.

Can I clear formatting before sorting a range using Office Scripts?

Yes. You can extract the format object of your target range using range.getFormat(). Once extracted, you can chain commands to reset horizontal or vertical alignments, clear text wrapping with setWrapText(false), and reset text orientation prior to running your sort logic.