Thoughts Brewing Blog

AI Quick Tips 30: Use ChatGPT to create Excel VBA macros

Written by Damien Griffin | Jul 11, 2024 1:27:00 PM

If you need Microsoft Excel functionality that is not built in then you may be able to use ChatGPT or a similar AI tool to create a VBA macro for you.


This tip is slightly more advanced because it requires a working knowledge of Excel and at least basic programming.  


Excel VBA (Visual Basic for Applications) is a tool used to create custom functions and automate repetitive tasks.  A macro is a set of programming instructions, and in Excel, they are written in VBA.  (*Note - there are other options like Python but we’re using VBA here because it is built in)


I’ll use a really simple example to illustrate this.


If you had all of your sales data in column A of an Excel spreadsheet and you wanted to add all of it up you could create a VBA macro to do it (let’s assume that you don’t know that Excel does this for you already…)


This was my prompt for this example

“Create a Microsoft Excel VBA macro called TotalSales that adds all of the values in column A and formats the sum into currency - dollars to 2 decimal places”


And here’s what I got back


Then, under the macro, it told me how to use it


Basic programming knowledge helps here because AI tools make mistakes and there is nothing saying that its code will be flawless (it won’t be…)  It will be easier for you to find what’s wrong and fix it if you know what you are looking at.


If you have a Microsoft Office 365 plan with Copilot that would probably be a better choice since it is already integrated with Excel.  I didn’t use that here because I wanted to show how you could do it without access to that paid tool.