I’ve been playing with ChatGPT for a while now and mark me impressed with its capabilities. One of the first things I did was ask it to create a VBA function that calculates the first of the month:

Can ChatGPT write VBA code? How about SQL Queries? Yes! But...

A couple of points on the code above:

  • I never named the function yet the AI used a great name: FirstOfMonth, notice the capitalization in the name.
  • I loved how it used proper indentation.
  • There is a Copy Code button on the upper right corner.
  • It provides an example of how to use the function in your code!

Can the AI create a subroutine?

I asked the AI to create a VBA program:

Can ChatGPT write VBA code? How about SQL Queries? Yes! But...

  • Notice how the AI declare each variable, uses a great name for each variable and at the end of the routine sets each object to nothing.
  • I’m impressed with the use of a recordset in the code.
  • Notice that is DID NOT dimension percentIncrease as String.
  • I would have preferred to use an action query instead of a record loop, which is much slower.

So not perfect but passable.

A VBA function with a parameter

I then asked the AI to create a VBA function that calculates the first or the last of the month based on the parameter passed. (Notice I keep the instructions to a minimum):

Can ChatGPT write VBA code? How about SQL Queries? Yes! But...

  • I loved how the AI dimensioned the input parameters correctly with the date and string variables.
  • Notice how it used Else condition to filter out any incorrect parameter and schools the user on using start or end.
  • Again, it offers an example on how to use it in your code at the end.

But…can it create a SQL Query?

So we established the AI can create code, but can it also create queries? I asked it to create a SQL query that will calculate the total sales by month and by a salesperson using a table called tblSales and a field called SalesPerson, and name the result as TotalSales:

Can ChatGPT write VBA code? How about SQL Queries? Yes! But...

I LOVED the AI explaining the query in the comments. But the query is using DATE_TRUNC which is not compatible with SQL Server, (the correct function is DATETRUNC). So I asked the AI to make the prior query compatible with SQL Server:

Can ChatGPT write VBA code? How about SQL Queries? Yes! But...

Again, the AI offers a lot of explanations that may benefit a novice programmer.

My Takeaways…and the potential for even more going forward

Usually, when a programmer needs help designing a function they would turn to Google and get a listing of websites that may or may not include the code or query you can use, and for sure it’s not going to have the variable names you wish to use. Having the AI use your variable names, come up with great function and subroutine names, although the code approach was not the best. So what does ChatGPT spell for the future of AIs? Will it replace VBA developers or consultants?

Can I tell the AI to write an inventory database in VBA using FIFO?

I actually did tell it to do just that, but it created a simple module using a collection to add, remove or sell inventory, not a full-fledged database, although to its credit it did say “Note: This is a very basic example and it’s recommended to use a database management system(DBMS) like MS Access, MySQL or SQL Server for a real-world inventory system.”

Will I continue to use Google? Yes, but only after I’ve tried ChatGPT.