Excel Online - Office script - renaming sheet based on cell value

yomarcos

New Member
Joined
Aug 13, 2021
Messages
39
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I need to create a script to change the name of a sheet based on A1 on that same sheet. I need this script to run automatically.

Is this doable?
Does anyone know how?

I tried and failed.

Thank you

Moderator Edit:
This is for xl Online, so VBA won't work
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi.

This script will rename the specified sheet according to what is in cell A1.
JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let worksheet = workbook.getWorksheet("Sheet 1");
    let newName = worksheet.getRange("A1").getValue().toString();
    worksheet.setName(newName);
}
Office scripts only support running on demand inside of excel online. Explain what you mean by "running automatically" - should it run on a schedule or only when some event happens? To run on a schedule, you'll need to use a different (and very handy) Microsoft product: Power Automate. From your script, click Create Flow. It will take you to the Power Automate site where you create the flow and choose your schedule and target file/script to run.

1638478173686.png
 
Upvote 0
Hi severynm,
Thank you for your answer.
I get an error though:
1638871269321.png


Also, I need this to work for all the sheets, not just for Sheet 1.

When I say "running automatically", I mean I need this to run like an Excel formula: all the time. But if it needs to be narrowed down, I need this to update all sheets starting from sheet 2 when there is a change in Column A, Sheet 1.
 
Upvote 0
I found a way to change the sheet name based on A1

JavaScript:
function main(workbook: ExcelScript.Workbook) {
  let ws = workbook.getWorksheets();
  for (let i = 1; i < ws.length; i++) {
    let NewName = ws.getCell(0, 0).getValue();
    ws.setName(NewName);
  }
}
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top