Building an Excel AI Assistant with a Local LLM: A Complete Guide from Deployment to Integration

    Learn how to build an Excel-based AI assistant powered by a locally deployed open-source LLM. This guide covers hardware requirements and model selection, setting up a local inference server, optionally adding a RAG knowledge base, and writing the VBA code that lets you ask questions and receive answers directly inside a spreadsheet—keeping all data private and eliminating per-call API costs.

    1. Background and Overall Approach

    As open-source large language models (LLMs) mature, more developers want to run their own AI assistants locally, driven by two core needs: data privacy and cost control. For roles that routinely handle sensitive information—finance, legal, operations—sending queries to public cloud services often creates compliance risks. Local deployment keeps all data confined to a personal machine or internal network.

    Among the many possible use cases, connecting an LLM to Excel is a classic entry point. Excel is a tool most enterprise users face every day. If you can ask questions directly inside a spreadsheet and get answers automatically, you cover a wide range of high-frequency needs: data interpretation, report generation, formula writing, and text polishing.

    The overall architecture of this solution has four layers:

    1. Inference layer: a local inference framework runs an open-source LLM and exposes a unified HTTP API;
    2. Augmentation layer (optional): a local knowledge-base Q&A framework attaches personal documents to the model, enabling retrieval-augmented generation (RAG) over private knowledge;
    3. Calling layer: an Excel VBA script invokes the local service via HTTP requests;
    4. Interaction layer: the user types a question into a designated cell and clicks a button; the answer is written back to a result cell automatically.

    2. Environment Setup and Model Selection

    2.1 Hardware and Model Configuration Recommendations

    Model selection requires a trade-off between your hardware and answer quality. A general reference:

    GPU VRAM / RAM Recommended Model Size Suitable Scenarios
    8GB or less 7B-class quantized models Everyday Q&A, simple text processing
    16GB 14B-class quantized models More complex reasoning and writing tasks
    24GB and above 32B-class or larger quantized models High-quality reasoning, code generation

    For the vast majority of office scenarios, a quantized 7B–14B model is smooth enough. Quantization compresses model size and memory footprint by reducing parameter precision, and it is the key technique for running LLMs on consumer hardware. A Q4-level quantized version usually strikes a good balance between quality and speed.

    2.2 Deploying a Local Inference Server

    Use a mature local inference framework to host the model. Such frameworks typically offer:

    • Model download and loading with a single command;
    • An OpenAI-style REST API by default, making third-party integration easy;
    • Multi-model management and switching.

    After deployment, verify from the command line that the service is running and confirm its listening port (commonly 11434 or similar). You can run a smoke test like this:

    1. curl http://localhost:11434/api/generate -d '{
    2. "model": "your-model-name",
    3. "prompt": "Hello, please introduce yourself"
    4. }'

    If generated content is returned normally, the inference service is ready.

    2.3 Optional: Setting Up a Local Knowledge-Base Q&A Framework

    If you want the model to answer questions based on personal documents (contracts, manuals, report specifications, etc.), you can additionally deploy an open-source knowledge-base Q&A framework. Its core workflow is:

    1. Split local documents into text chunks;
    2. Vectorize the chunks with an embedding model and store them in a vector database;
    3. When a user asks a question, retrieve the most relevant text fragments first, then append them to the prompt sent to the LLM for answer generation.

    During configuration, enter the local inference server’s API address and model name in the framework’s model settings, create a workspace, and generate an API key inside the framework for later use by Excel. The framework typically listens on a local port by default (e.g., 3001), and the VBA script will call the chat endpoint exposed on that port directly.

    3. Excel-Side Development: VBA Implementation

    3.1 Worksheet Layout

    Design a clear three-part layout on the worksheet:

    • Cell A2: question input area;
    • A button: bound to a macro as the trigger;
    • Cell A4: result output area—adjust row height and column width as needed so long text remains readable.

    3.2 Core Code

    Press Alt + F11 to open the VBA editor, insert a module, and write the following code (using the knowledge-base framework’s streaming chat endpoint as an example):

    1. Sub CallLocalLLM()
    2. Dim question As String
    3. Dim response As String
    4. Dim url As String
    5. Dim apiKey As String
    6. Dim http As Object
    7. ' Read the question
    8. question = ThisWorkbook.Sheets(1).Range("A2").Value
    9. ' Endpoint URL and API key (workspace name must match what you created, in lowercase)
    10. url = "http://localhost:3001/api/v1/workspace/your-workspace/stream-chat"
    11. apiKey = "the API key you generated in the framework"
    12. Set http = CreateObject("MSXML2.XMLHTTP")
    13. http.Open "POST", url, False
    14. http.setRequestHeader "accept", "text/event-stream"
    15. http.setRequestHeader "Content-Type", "application/json"
    16. http.setRequestHeader "Authorization", "Bearer " & apiKey
    17. ' Build the request body; mind the quote escaping in JSON
    18. Dim body As String
    19. body = "{""message"":""" & question & """,""mode"":""chat""}"
    20. http.send body
    21. If http.Status = 200 Then
    22. response = http.responseText
    23. ' Parse the text content from the streaming response and write it to the result cell
    24. ThisWorkbook.Sheets(1).Range("A4").Value = ParseStreamResponse(response)
    25. Else
    26. MsgBox "Request failed, status code: " & http.Status, vbExclamation
    27. End If
    28. End Sub

    Several implementation details are worth expanding on:

    (1) JSON escaping. If the user’s question contains double quotes or line breaks, direct string concatenation produces an invalid request body. The rigorous approach is to escape the question before concatenation:

    1. question = Replace(question, """", "\""")
    2. question = Replace(question, vbLf, "\n")

    (2) Parsing the streaming response. Because the request header declares text/event-stream, the returned content is a text stream of multiple data: event lines. You need a parsing function that extracts the JSON fragment from each line, pulls out the text field, and concatenates the pieces into a complete answer. If the endpoint also supports non-streaming mode, you can simplify this to parsing a single JSON object.

    (3) Improving the synchronous-call experience. The code above uses a synchronous request (the third argument of Open is False), so Excel appears unresponsive during the call. Model inference typically takes several seconds to tens of seconds, so it is advisable to show a “Generating, please wait” prompt when the button is clicked, or switch to asynchronous callbacks via WinHttp for a better experience.

    3.3 Binding the Button and Distribution

    Back on the worksheet, bind the macro to a button via “Developer — Insert — Button”. If you plan to distribute the workbook to colleagues, note that each recipient’s machine must also have the model and service framework deployed, and macro permissions must be enabled for the VBA project.

    4. Extension Ideas and Best Practices

    • Batch processing: extend single Q&A into a loop that iterates over multiple rows of questions, enabling automatic summarization or translation of an entire column of data;
    • Prompt engineering: fixing a system-level prompt in the request body (e.g., “You are a financial analysis expert; please answer concisely”) can significantly improve the stability and professionalism of the answers;
    • Combining cloud capabilities: for machines with insufficient compute, the inference layer can be replaced with a cloud-hosted model API—the code structure stays exactly the same; only the URL and key change. You can also pair this with cloud container services or serverless functions to deploy a middle layer for team-level shared access;
    • Logging and monitoring: it is recommended to write the timestamp, question, and answer of each call to a hidden worksheet in VBA, enabling later auditing and effect tracking.

    5. Conclusion

    With very little code (roughly a few dozen lines of VBA), this solution connects the full chain of “local LLM — knowledge-base framework — Excel”, demonstrating the practical value of locally deployed LLMs in office automation scenarios. It not only solves the compliance problem of keeping data inside the internal network, but also removes dependence on metered public API billing. For developers, the same pattern can be smoothly migrated to Word, email clients, or even internal systems, becoming a general paradigm for building private AI capabilities.