VBA & Macro Problems

How to Activate the First Worksheet with an Excel Office Script

John WilsonJohn Wilson Nov 10, 2026 869 views

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.

How to Activate the First Worksheet with an Excel Office Script
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the Code Editor

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.

2
Define the First Worksheet Variable

Declare a variable to retrieve the first sheet by adding this line at the very end of your script: let sht = workbook.getFirstWorksheet();

3
Activate the Worksheet

Call the activate method on the newly defined variable by adding the following line immediately after: sht.activate();

4
Save and Run

Save your script modifications and click Run. The workbook will now automatically switch to the first worksheet upon completion of the automation.

Use the getFirstWorksheet() Method in Your Office Script
Platform Limitations: Office Scripts do not provide workbook-level events (like 'on open') in the same way as Office.js. If you run this script in the background via Power Automate, UI-level actions such as activating a worksheet may not persist when a user subsequently opens the file.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website and download the free WPS Office installer to your device.
  2. 2. Install the Software: Run the installer and follow the quick on-screen instructions to set up WPS Office.
  3. 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.
Seamlessly compatible with Microsoft Excel formats (.xlsx, .xls, .csv).Built-in JS Macros and VBA support to automate your spreadsheet tasks without a premium subscription.Free, lightweight, and incredibly fast to install and launch.Familiar user interface ensuring zero learning curve for Excel users.
microsoft office alternative - wps office

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.