Write Excel Office Scripts with ChatGPT

Mynda Treacy

July 25, 2023

Excel Office Scripts: The Future of Automation, Replacing VBA

Excel Office Scripts have ushered in a new era of automation, gradually replacing Visual Basic for Applications (VBA/macros) as the preferred method for automating Excel tasks in both the desktop and online.


VBA’s limitation is it’s tied to the desktop. VBA cannot and never will be able to execute in Excel online.


With Office Scripts, users can record and replay actions, eliminating the need for complex VBA coding and making automation more accessible to a wider range of users.

Another way to write Office Scripts is to get ChatGPT to do it for you!

Join me as we explore the exciting world of Excel Office Scripts and witness the future of automation unfold, saying goodbye to cumbersome code and embracing a user-friendly and efficient way to automate BORING Excel tasks.


Watch the Excel Office Scripts Video

Subscribe YouTube


Download Workbook & Office Script

Enter your email address below to download the files.

By submitting your email address you agree that we can email you our Excel newsletter.

Excel Office Script Requirements

Office Scripts is available to Excel users with the following:

  1. Excel for Windows (version 2210 or higher), Excel for Mac, or Excel on the web.
  2. OneDrive for Business.
  3. Any commercial or educational Microsoft 365 license with access to the Microsoft 365 Office desktop apps, such as:
  • Office 365 Business or Business Premium
  • Office 365 ProPlus or ProPlus for Devices
  • Office 365 Enterprise E3 or E5
  • Office 365 A3 or A5
  1. Internet connectivity with connected experiences

Where to Find Office Scripts Tools

The Office Scripts tools are available on the new Automate tab of the ribbon:

Automate tab on Excel Ribbon

On the Automate tab you can record scripts using the ‘Record Actions’ button or write a new script by hand, using the ‘New Script’ button. This opens the Code Editor pane:

Code Editor Pane

In the gallery you’ll find a selection of sample scripts to try, as well as any scripts you’ve recently created.

The ‘All Scripts’ button opens the Code Editor pane where you can navigate to recent scripts, scripts saved in this workbook and samples:

navigate to recent office scripts

If you don’t see this tab, check your version of Excel meets the requirements above.

Office Script Scenario

A table of contents with hyperlinks to each sheet can make large files easy to navigate for your users.

However, they can be time consuming to create and if you change the name of any sheets, you need to manually edit the hyperlinks.

table of contents on Excel sheet

With Office Scripts we can automate the creation of tables of contents and update them for any changes in the file with one click.

Using ChatGPT to Write Office Scripts

I could have recorded the script, but I would have had to edit it to handle deleting any existing tables of contents in the file before I could update it with a new one.

And with zero JavaScript skills, that’s way beyond my capabilities, but not for ChatGPT.

The key with using ChatGPT to write Office Scripts is to give it a very clear description of what you want.

The ChatGPT prompt I used was:

Write an Excel Office Script to generate a new Table of Contents sheet called "TOC" that contains a hyperlink to each sheet in the current file. If there is already a sheet called "TOC", delete it before creating the new sheet. Start the table of contents hyperlinks in cell B3. Format the hyperlinks in blue font with an underline. Place a heading in cell B1 of the "TOC" sheet with the text "Table of Contents" in black bold font, size 18. Hide the gridlines on the “TOC” sheet. Annotate the script to explain what each step in the script does.

It got a little stuck on the font formatting, but nothing that it couldn’t correct once I fed the errors back into ChatGPT.

The end result is this code:

Office Script created by ChatGPT for Excel

Inserting the Office Script into Excel

To use the Excel Office Script returned by ChatGPT, follow these steps:

  1. Open the Excel file you want to add the Table of Contents to.
  2. On the "Automate" tab
  3. Click on "New Script" to open the Office Scripts editor.
  4. Replace the default script with the provided script.
  5. Click the "Run" button or press "Ctrl + Enter" to execute the script.

Insert Office Script into Excel

Once you’re happy the script runs successfully, click the ‘Save Script’ button to the right of the ‘Run’ button.

Note: to rename the script, click on the name in the Code Editor to bring up the Script name edit box:

Rename Office Script

Re-using Office Scripts

By default, scripts are saved to your OneDrive account and are available in the script gallery or your script library via the ‘All Scripts’ button on the ribbon.

To attach the script to the current file and share it with anyone who has edit access to the file, you can link to it via a button in the current file.

  1. Go to the sheet you want the button placed on
  2. Automate tab > All Scripts
  3. Click the ellipsis for the script you want to link to in the file > + Add in workbook:

Add Office Script to Excel workbook

This adds a green button to the current sheet with the name of the script. When clicked, the script will execute:

Button in Excel to run Office Script

Go ahead and give Office Scripts a go. Try recording your own and for more complex scripts, reach out to ChatGPT for help.

16 thoughts on “Write Excel Office Scripts with ChatGPT”

  1. Using ChatGPT to write Office Scripts often results in methods incompatible with Excel’s Office Scripts, likely due to similarities with TypeScript and Java. This leads to frequent error messages when running scripts. While Copilot may help improve the situation, it’s currently better suited to TypeScript. As of now, Office Scripts can’t trigger on events directly, but Power Automate can be used to manage this. While Copilot and LLM advancements are promising, ordinary users might still face limitations, such as licensing restrictions and inconsistent results from AI systems, which vary in output even with similar prompts. Hopefully, these tools will continue evolving to meet all users’ needs.

    Reply
  2. Using Chatgpt to write office scripts,it always gives me some type methods that cannot be used in Excel office scripts. Since it is similar to typescript and Java, I think it sometimes may mix up. Get lots of error messages when running the scripts. Wonder how to solve this situation.

    Reply
  3. The licensing requirement seem to indicate that this is ONLY for businesses. Is it likely that ‘ordinary’ users like me will ever get access?

    Reply
  4. Wow! Blown away by how fast you created something so useful. Thank you for sharing the example. I was able to take what you provided in the sample file and make a slight modification to add additional information to the TOC worksheet with just a few iterations using ChatGPT.

    Reply
  5. The big difference between VBA and Typescript is that, as far as we know, Typescript can monitor events in the spreadsheet to trigger code execution, whereas VBA can do so. This is a limitation for us. Unless you know otherwise

    Reply
    • Hi Paul, I think you meant that Office Scripts ‘can’t’ trigger on events, which is correct. However, you can team it with Power Automate to handle the trigger and execution of the script.

      Reply
  6. Mynda,
    I look forward to your posts and today’s installment exceeded all expectations. I never really thought of scripts outside of my workbooks since I write code for my purposes alone. However, you opened a new door that I need to explore more. Thank you for your continued posts as you add value to my day!
    Alan

    Reply

Leave a Comment

Current ye@r *