How to Fix Excel Office Script Not Sorting by Fill Color
Question details
The user needs to correctly automate sorting data by cell fill color using an Excel Office Script triggered by a button.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Running a recorded Office Script to move specific colored cells (e.g., red YES cells) to the bottom of a worksheet.
- Observed behavior
- The Office Script recorded from the 'Sort by Color' command fails to correctly move the colored cells to the bottom when executed via a button.
Verify that your cell colors are applied as standard background fills, as Office Scripts may fail to recognize colors applied dynamically through Conditional Formatting.
Seek Support in the Microsoft Office Development Community
Because Office Scripts rely on TypeScript and specific Microsoft 365 environments, complex script failures are best addressed by Microsoft's dedicated developer community.
Recorded Office Scripts often struggle with dynamic ranges or specific color index translations. Consulting experts ensures your TypeScript logic is correctly modified to handle custom color sorting.
Copy the exact code of your recorded Office Script, and note your expected sort order, specific fill-color details, and the platform (Excel for Web or Desktop) you are using.
Open your web browser and go to the Office Development section of Microsoft Q&A, which actively handles Office Scripts troubleshooting.
Create a new thread providing all your gathered details so the community can help correct the script's sorting logic.
Use the Built-in Custom Sort Feature
If automation is failing, you can achieve the exact same result manually using Excel's robust built-in Custom Sort tool.
Use VBA Instead of Office Scripts
For desktop versions of Excel, traditional VBA macros often record and execute color-sorting commands more reliably than web-based Office Scripts.
Try WPS Office for Seamless Macro and Data Automation
If Excel's Office Scripts are causing frustration, WPS Office offers a powerful, free alternative. With robust support for traditional VBA and modern JS Macros, you can easily automate sorting and data formatting without the steep learning curve.
- 1. Download and install: Get WPS Office for free from the official website and install it on your device.
- 2. Open your spreadsheet: Open your existing .xlsx or .xlsm file directly in WPS Spreadsheet with full formatting retained.
- 3. Automate your tasks: Use the built-in Macro editor to create reliable sorting scripts using JSA or VBA.

Frequently Asked Questions
Why does my recorded Office Script fail to sort by color?
Recorded scripts often hardcode specific ranges or fail to accurately capture color index values. Dynamic ranges or colors applied via conditional formatting usually require custom TypeScript code rather than simple macro recording.
Can I sort by cell color without using any scripts?
Yes. You can select your data range, go to the Data tab, click Sort, and change the 'Sort On' parameter to 'Cell Color'. From there, you can choose the color and dictate whether it appears at the top or bottom.
What is the difference between Office Scripts and VBA?
Office Scripts are based on TypeScript and are primarily designed for Excel on the Web to automate tasks across cloud platforms. VBA (Visual Basic for Applications) is the traditional desktop macro language for Office applications.




