Here's a VBA solution that meets your requirements. This code creates an interactive AI-like interface in Excel:
' Place in ThisWorkbook module
Private Sub Workbook_Open()
CreateButton
Application.OnKey "%k", "ToggleSourceData"
End Sub
' Place in a standard module
Sub CreateButton()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
On Error Resume Next
ws.Buttons("V AI").Delete
On Error GoTo 0
Dim btnLeft As Double
Dim btnTop As Double
btnLeft = ws.Range("B2").Left
btnTop = ws.Range("B2").Top
Dim btn As Button
Set btn = ws.Buttons.Add(Left:=btnLeft, Top:=btnTop, Width:=100, Height:=30)
With btn
.Caption = "V AI"
.Name = "V AI"
.OnAction = "Button_Click"
End With
End Sub
Sub Button_Click()
Dim userName As String
userName = InputBox("What is your name?", "V AI")
If userName = "" Then Exit Sub
Dim query As String
query = InputBox("What can I do for you?", "V AI")
If LCase(query) = "i want to quit" Then
ThisWorkbook.Sheets("Sheet1").Range("A1").Value = "Goodbye, " & userName & "!"
Exit Sub
ElseIf query = "" Then Exit Sub
Dim response As String
response = GetWebData(query)
StoreSourceData response
ThisWorkbook.Sheets("Sheet1").Range("A1").Value = response
End Sub
Function GetWebData(query As String) As String
On Error Resume Next
Dim http As Object
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", "https://api.quotable.io/random?tags=" & query, False
http.send
If http.Status = 200 Then
GetWebData = "AI Response: " & Split(http.responseText, """content"":""")(1)
Else
GetWebData = "Error connecting to server"
End If
Set http = Nothing
On Error GoTo 0
End Function
Sub StoreSourceData(data As String)
Dim ws As Worksheet
On Error Resume Next
Set ws = ThisWorkbook.Sheets("SourceData")
On Error GoTo 0
If ws Is Nothing Then
Set ws = ThisWorkbook.Sheets.Add
ws.Name = "SourceData"
ws.Visible = xlSheetVeryHidden
End If
ws.Range("A1").Value = data
End Sub
Sub ToggleSourceData()
Dim ws As Worksheet
On Error Resume Next
Set ws = ThisWorkbook.Sheets("SourceData")
On Error GoTo 0
If Not ws Is Nothing Then
ws.Visible = IIf(ws.Visible = xlSheetVisible, xlSheetVeryHidden, xlSheetVisible)
Else
MsgBox "No source data available", vbInformation
End If
End Sub
To use this code:
- Open Excel and press
Alt + F11 to open the VBA editor
- Insert a new module (Right-click in Project Explorer > Insert > Module)
- Paste the code into the appropriate modules:
- The
Workbook_Open and CreateButton go in ThisWorkbook module
- The rest go in a standard module
- Save the workbook as a macro-enabled (.xlsm) file
- Close and reopen the workbook to activate the button
Features:
- Click the "V AI" button to start interaction
- First asks for your name
- Then asks what you need help with
- Press
Alt+K to toggle source data visibility
- Uses a public API (quotable.io) for demonstration
- Responses appear in cell A1
- Type "I want to quit" to exit
The code uses a public quote API for demonstration. To modify for real AI responses, you would need to:
- Replace the API endpoint in
GetWebData
- Adjust the response parsing logic
- Add proper authentication if needed
Note: This uses a free API endpoint for demonstration. The actual response format may vary depending on the API used.