How to Activate the First Worksheet with an Excel Office Script
Question details
The user needs an Office Script to ensure the first worksheet is set as the active sheet after running an automation, so the workbook opens at the intended starting point.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating workbook generation or modification using Office Scripts.
- Observed behavior
- Without a specific script command, the workbook remains on the last active worksheet instead of defaulting to the first sheet upon completion.
Ensure you have an active Microsoft 365 subscription that supports Office Scripts, and verify whether you are using Office Scripts (TypeScript) or Office.js, as their event handling capabilities differ.
Use the getFirstWorksheet() Method in Your Office Script
Add a few lines of TypeScript code at the end of your Office Script to retrieve and activate the first worksheet.
Office Scripts use TypeScript to interact with the Excel workbook. By capturing the first worksheet object and explicitly calling the activate method at the end of your script, you force the UI to switch to that sheet when the script completes.
In Excel on the web or Excel for Windows/Mac, navigate to the Automate tab on the ribbon and open your existing Office Script in the Code Editor pane.
Declare a variable to retrieve the first sheet by adding this line at the very end of your script: let sht = workbook.getFirstWorksheet();
Call the activate method on the newly defined variable by adding the following line immediately after: sht.activate();
Save your script modifications and click Run. The workbook will now automatically switch to the first worksheet upon completion of the automation.

Try WPS Office for Powerful Macro & Spreadsheet Automation
While Excel's Office Scripts require a paid Microsoft 365 subscription, WPS Office offers robust, built-in support for both VBA and its own powerful JS Macros for free. It's a lightweight, easy-to-use alternative with full format compatibility.
- 1. Download WPS Office: Visit the official WPS website and download the free WPS Office installer to your device.
- 2. Install the Software: Run the installer and follow the quick on-screen instructions to set up WPS Office.
- 3. Open Your Spreadsheet: Launch WPS Spreadsheet and open your existing Excel (.xlsx) file to explore the built-in JS Macro capabilities for your automation needs.

Frequently Asked Questions
What is the difference between Office Scripts and Office.js?
Office Scripts are typically used by end-users to automate repetitive tasks within Excel on the web and desktop using TypeScript, whereas Office.js is a broader developer API used to build comprehensive Office Add-ins.
Can I trigger an Office Script to run automatically when the workbook opens?
No, Office Scripts do not currently support workbook-level events like 'on open'. You must manually run the script, trigger it via a customized button in the worksheet, or automate it using Power Automate.
Why doesn't my worksheet activation persist after running the script via Power Automate?
If you are running the script through a background automation platform like Power Automate, UI-level actions such as activating a worksheet do not affect the saved state of the file. Activating a sheet only affects the current active session in the Excel UI.
How do I activate a worksheet by its name instead of its index?
You can use the getWorksheet() method instead of getFirstWorksheet(). For example, add the line: let sht = workbook.getWorksheet('SheetName'); followed by sht.activate(); to switch to a specific sheet.




