Skip to content

Understand Office VBA code with ChatGPT

If you have Office VBA you’re trying to understand, ChatGPT can help.  ChatGPT can take some VBA code and explain it in plain human language.

This is very useful for digging into poorly documented or undocumented code.  Also for beginners trying to get a handle on how Office VBA works.

Office Watch has already explained with Easily learn and make Office VBA with ChatGPT which takes a plain English request and converts it into VBA code.

Explain Office VBA line by line in ChatGPT

It’s easy to do at https://chat.openai.com/ .

Type in:

“Explain this Office VBA code, line by line”

Then paste the VBA code.

Use the phrase “Office VBA” so ChatGPT knows which programming language to interpret.  ChatGPT ‘understands’ several common programming languages.

We got better results adding the phrase “line by line”. Without that, ChatGPT might just give a summary of what the code does.

Here’s some examples of ChatGPT explaining Office VBA

Outlook VBA code explained

We asked ChatGPT to explain this Outlook VBA code which searches the Inbox for a sender name.

ChatGPT’s response:

This Office VBA code creates a macro called “OpenMostRecentEmailFromFred” that can be run in Microsoft Outlook.

  1. The first line of the code declares a variable called “objOutlook” as an object of the Outlook application.
  2. The second line declares a variable called “objNamespace” as an object of the Outlook namespace.
  3. The third line declares a variable called “objFolder” as an object of the Outlook MAPIFolder.
  4. The fourth line declares a variable called “objItem” as an object of the Outlook MailItem.
  5. The fifth line sets the “objOutlook” variable to the Outlook application.
  6. The sixth line sets the “objNamespace” variable to the MAPI namespace of the Outlook application using the “objOutlook” variable.
  7. The seventh line sets the “objFolder” variable to the default inbox folder of the Outlook application using the “objNamespace” variable.
  8. The next line starts a “For Each” loop that goes through each item in the “objFolder” variable.
  9. Inside the loop, there is an “If” statement that checks if the sender name of the current item in the loop is “Fred”.
  10. If the sender name is “Fred”, the next line opens the email using the “objItem.Display” method.
  11. The next line exits the loop.
  12. The end of the loop and the end of the macro is marked by the “Next” and “End Sub” lines respectively.

Excel VBA explained

Another example, using some Excel VBA from an Office Watch article about getting the difference between GMT/UTC and local time.

From ChatGPT:

This Office VBA code creates a function called “GetLocalToGMTDifference” that returns the difference between the local time zone and the Greenwich Mean Time (GMT) in decimal hours.

  1. The first line of the code declares the function and specifies that it returns a single data type.
  2. The next few lines are constant declarations that are used in the code to identify the time zone type.
  3. The following lines declare variables: “TimeZoneInf” as a TIME_ZONE_INFORMATION data type, “Ret” as a single data type, and “Diff” as a single data type.
  4. The next line calls the “GetTimeZoneInformation” function and stores the result in the “Ret” variable.
  5. The “Diff” variable is set to the negative of the bias value from the “TimeZoneInf” variable. This bias value represents the minutes difference between the local time and GMT.
  6. The function then returns the “Diff” variable value.
  7. The “If” statement checks if the “Ret” variable value is equal to the constant value of “TIME_ZONE_ID_DAYLIGHT”.
  8. If the condition is true, then the code checks if the month value of the daylight date is not equal to 0.
  9. If this condition is also true, the “Diff” variable value is reduced by the daylight bias, which represents the additional time difference between the local time and GMT during daylight saving time.
  10. The next line is commented out and would have multiplied the “Diff” variable value by 60 to convert the time difference from minutes to seconds.
  11. The “Diff” variable value is then divided by 1440 to convert the time difference from minutes to decimal hours.
  12. The function then returns the “Diff” variable value.
  13. The end of the function is marked by the “End Function” line.

Easily learn and make Office VBA with ChatGPT
More adventures with Office VBA and ChatGPT
Word documents might write themselves and more

About this author

Office-Watch.com

Office Watch is the independent source of Microsoft Office news, tips and help since 1996. Don't miss our famous free newsletter.