Skip to content

Activate a worksheet

This article explains how to use Office Scripts to make a specific worksheet active, with sample code.

Use the activate method of the ExcelScript.Worksheet object.

ExcelScript.Worksheet.activate(): void;

It has no arguments and no return value. When you run it, the target worksheet becomes active in Excel.

This sample code specifies a sheet name and makes the target sheet active.

If the target sheet isn’t found, it throws an error.

function main(workbook: ExcelScript.Workbook) {
/** Target sheet name */
const sheetname = 'Sheet1';
/** Target worksheet */
const sheet = workbook.getWorksheet(sheetname);
if (!sheet) {
throw `There is no worksheet named ${sheetname}`;
}
// Activate the worksheet
sheet.activate();
}