Below is a complete VBA code implementation based on your requirements. I'll explain it briefly first (but the code itself is self-contained and ready to paste into an Excel VBA module).
Key Features Implemented:
- Button Creation: A macro
CreateVAIButton creates a button labeled "V AI" on the active sheet. It's positioned approximately in the "middle" of the visible sheet area (centered horizontally and vertically based on typical screen sizes; adjust if needed).
- Interaction Flow:
- When the "V AI" button is clicked, it asks for your name via an InputBox.
- If a name is entered, it enters a loop asking "What can I do for you?" (via InputBox).
- Type your query, and it fetches a response from the web (using a public API like Wikipedia's summary endpoint for simplicity, mimicking ChatGPT-like responses by summarizing info).
- Responses are output directly to the active Excel sheet (in columns A and B, starting from row 1, with query and response clearly labeled).
- To quit, type exactly "i want to quit" (case-insensitive).
- Web Data Handling:
- Data is fetched from the web silently (no websites or URLs shown).
- No source data (e.g., raw API response or URL) is shown in the output.
- Show Source:
- Press Alt+K to reveal sources (it unhides a hidden sheet named "Sources" that logs the query, response, and hidden source URL/API details).
- The sources sheet is hidden by default.
- Other Notes:
- This is a simple "AI-like" simulation using web fetches (not true AI, but responds intelligently to queries by summarizing web info).
- If the web fetch fails, it shows a polite error in the response.
- Run
CreateVAIButton first to add the button. Then click it to start.
- The code uses
MSXML2.XMLHTTP for web requests (built-in to VBA, no external dependencies).
- For JSON parsing (from API), a simple string extraction is used (keeps it lightweight; assumes Wikipedia API format).
- Hotkey (Alt+K) is set up automatically when the workbook opens (via
Workbook_Open in ThisWorkbook).
How to Use:
- Open Excel VBA Editor (Alt+F11).
- Insert a new Module and paste the code below.
- In the ThisWorkbook object, add the
Workbook_Open sub (as shown).
- Save the workbook as macro-enabled (.xlsm).
- Run the macro
CreateVAIButton (via Developer tab or Alt+F8) to create the button.
- Click the "V AI" button on the sheet to start.
- Press Alt+K anytime to show/hide sources.
VBA Code:
' Module Code (Insert into a new Module)
Option Explicit
Dim SourceSheet As Worksheet
Sub CreateVAIButton()
Dim btn As Shape
Dim ws As Worksheet
Set ws = ActiveSheet
' Remove existing button if any
On Error Resume Next
ws.Shapes("VAIButton").Delete
On Error GoTo 0
' Create button shape
Set btn = ws.Shapes.AddShape(msoShapeRoundedRectangle, 300, 200, 100, 50) ' Approx middle: adjust Left/Top for your screen
btn.Name = "VAIButton"
btn.TextFrame.Characters.Text = "V AI"
btn.TextFrame.HorizontalAlignment = xlHAlignCenter
btn.TextFrame.VerticalAlignment = xlVAlignCenter
btn.Fill.ForeColor.RGB = RGB(0, 112, 192) ' Blue background
btn.Line.Visible = msoFalse
btn.OnAction = "VAIButton_Click"
' Center it better (based on visible range)
btn.Left = (ws.Columns("A").Width + Application.UsableWidth) / 2 - btn.Width / 2
btn.Top = (Application.UsableHeight) / 2 - btn.Height / 2
MsgBox "V AI button created. Click it to start."
End Sub
Sub VAIButton_Click()
Dim name As String
name = InputBox("What is your name?")
If Trim(name) = "" Then Exit Sub
Dim query As String
Dim rowNum As Long
rowNum = 1 ' Start output at row 1
Do
query = InputBox("Hello " & name & ", what can I do for you?")
If LCase(Trim(query)) = "i want to quit" Then Exit Do
If Trim(query) = "" Then Exit Do
' Get response from web
Dim response As String
response = GetWebResponse(query)
' Output to active sheet (no sources shown)
With ActiveSheet
.Cells(rowNum, 1).Value = "Query: " & query
.Cells(rowNum + 1, 1).Value = "Response: " & response
rowNum = rowNum + 3 ' Space out responses
End With
' Log to hidden sources sheet (not shown unless Alt+K)
LogSource query, response, "https://en.wikipedia.org/api/rest_v1/page/summary/" & Replace(query, " ", "_")
Loop
End Sub
Function GetWebResponse(query As String) As String
On Error GoTo ErrHandler
Dim url As String
url = "https://en.wikipedia.org/api/rest_v1/page/summary/" & Replace(query, " ", "_")
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
xmlhttp.Open "GET", url, False
xmlhttp.Send
If xmlhttp.Status = 200 Then
Dim jsonText As String
jsonText = xmlhttp.responseText
' Simple extract of 'extract' field from JSON (mimics AI summary)
Dim startPos As Long, endPos As Long
startPos = InStr(jsonText, """extract"":""") + 11
endPos = InStr(startPos, jsonText, """}")
If startPos > 11 And endPos > startPos Then
GetWebResponse = Mid(jsonText, startPos, endPos - startPos)
Else
GetWebResponse = "Sorry, I couldn't find a clear summary for that."
End If
Else
GetWebResponse = "Sorry, there was an issue fetching the info. Try again!"
End If
Exit Function
ErrHandler:
GetWebResponse = "Error: Could not connect to web service."
End Function
Sub LogSource(query As String, response As String, sourceURL As String)
If SourceSheet Is Nothing Then
Set SourceSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
SourceSheet.Name = "Sources"
SourceSheet.Visible = xlSheetVeryHidden ' Hidden by default
SourceSheet.Range("A1:C1").Value = Array("Query", "Response", "Hidden Source")
End If
Dim lastRow As Long
lastRow = SourceSheet.Cells(SourceSheet.Rows.Count, 1).End(xlUp).Row + 1
SourceSheet.Cells(lastRow, 1).Value = query
SourceSheet.Cells(lastRow, 2).Value = response
SourceSheet.Cells(lastRow, 3).Value = sourceURL
End Sub
Sub ShowSource()
If SourceSheet Is Nothing Then
MsgBox "No sources logged yet."
Exit Sub
End If
If SourceSheet.Visible = xlSheetVeryHidden Then
SourceSheet.Visible = xlSheetVisible
SourceSheet.Activate
MsgBox "Sources shown. Press Alt+K again to hide."
Else
SourceSheet.Visible = xlSheetVeryHidden
MsgBox "Sources hidden."
End If
End Sub
' ThisWorkbook Code (Insert into ThisWorkbook object)
Private Sub Workbook_Open()
Application.OnKey "%k", "ShowSource" ' Alt+K to show/hide sources
End Sub
This should work as described. If you query something like "Eiffel Tower", it will fetch and summarize from the web, output to Excel, and hide all sources unless you press Alt+K. Adjust as needed!