This repository contains a VBA script that enables Excel to interact with the OpenAI ChatGPT API. The script implements a custom Excel function, GPT()
, which allows you to send queries to the ChatGPT model and receive responses directly within your Excel sheets.
Before you can use the GPT()
function in Excel, ensure the following requirements are met:
- Microsoft Excel 2016 or later with VBA support.
- An active OpenAI API key.
Follow these steps to set up and use the ChatGPT Excel integration:
To access the VBA tools needed for this project, you must first enable Developer Mode:
- Open Excel.
- Go to
File
>Options
>Customize Ribbon
. - In the right column, ensure the "Developer" checkbox is selected.
- Click
OK
to save your settings.
- Open Excel and access the VBA editor by pressing
ALT + F11
. - Right-click on
VBAProject (YourWorkbookName)
in the left sidebar, selectInsert
, and thenModule
. - Import the
ChatGPT.bas
file into the module.
- In the VBA editor, go to
Tools
>References
. - Check
Microsoft Scripting Runtime
to enable dictionary and collection support. - Ensure that
Microsoft XML, v6.0
(or similar version) is checked to handle HTTP requests.
The script requires VBA-JSON for parsing JSON responses from the ChatGPT API. Follow these instructions to include it:
- Download the latest version of VBA-JSON from VBA-JSON on GitHub.
- Import the
JsonConverter.bas
file into your Excel VBA project via the VBA editor.
- Enter a test query in a cell in Excel, for example:
=GPT("What is the capital of France?", [Cell with API_KEY])
- Ensure macros are enabled when you open the workbook.
The script includes basic error handling to manage potential issues with network requests or API responses. If an error occurs, the function will return a descriptive error message.
You can use the GPT() function anywhere in your Excel workbook:
=GPT("Your question here", [Cell with API_KEY])
=GPT("What is the capital city of " & A1, [Cell with API_KEY])
This function concatenates text with the contents of cell A1 to form the query.
Contributions are welcome. Please fork the repository and submit pull requests with your improvements.
MIT License
-
OpenAI for the API.
-
Contributors to the VBA-JSON project for JSON parsing capabilities.