Automate Excel with AI (VBA)
Excel is everywhere in enterprises. In this hands-on workshop, you will describe tasks, let AI generate VBA macros, and produce a shareable .xlsm file. No manual coding — just logic and results.
- Macro = A saved sequence of actions Excel can repeat automatically
- VBA = Visual Basic for Applications — Excel's built-in programming language for creating macros
- .xlsm file = An Excel file with macro support (regular .xlsx files cannot run macros)
- Copilot = An AI assistant that writes code based on your descriptions
From Ideas to Apps: No Manual Coding
Like all workshops in this course, you won't write code manually. You'll describe what you want, let AI generate the automation code, and review & iterate until it's perfect.
The AI does the coding — you focus on the logic and results.
Final Deliverable
A single .xlsm file containing your data and macros. Ready to run and share at work.
Understanding Macros & VBA
What are Macros?
Recorded sequences of actions in Excel. Excel generates VBA behind the scenes.
What is VBA?
Excel's automation language. Supports logic, loops, conditions, and more.
Why Master VBA?
For complex macros beyond recording — dynamic ranges, error handling, multi-step automation.
Editing Beats Re-recording
Update code instead of re-recording. Faster, reliable, scalable.
Try It Yourself: See VBA Code Behind Any Macro
Want to peek behind the curtain? You can view the VBA code of any macro in Excel:
Press Alt + F11 on Windows. On Mac, use the menu: Tools → Macro → Visual Basic Editor (or Option + F11 if your Excel version supports it).
In the left panel, expand VBAProject → Modules. Double-click any module to see its code.
Each Sub is a macro. Comments start with ' and explain what the code does.
Recording vs. Writing VBA
| Recording a Macro | Writing/Editing VBA (with AI) |
|---|---|
| Simple repetitive tasks | Complex logic (if/else, loops) |
| Fixed actions only | Dynamic behavior |
| Must re-record to change | Edit the code |
| Limited capabilities | Unlimited possibilities |
Real-World Use Cases
Here are common scenarios where Excel automation saves hours of manual work — exactly what you'll learn to build:
Financial Reports
Automatically consolidate monthly data from multiple sheets, calculate KPIs, and format the final report — every month, in seconds.
Data Cleaning
Remove duplicates, standardize date formats, trim whitespace, and flag invalid entries across thousands of rows.
Automated Emails
Generate and send personalized emails from Excel data — order confirmations, reminders, or status updates.
Dynamic Charts
Create charts that update automatically when new data is added — no manual selection or reformatting needed.
Why AI Changes the Game
What You'll Be Able To Do
After this workshop, you'll have the skills to automate tasks like these:
Our Tech Stack & Workflow
Here's how we combine tools you already know into a smooth workflow:
VS Code
The editor you already know. You'll write .bas files here.
GitHub Copilot
Generates VBA from your descriptions in comments.
VBA Modules (.bas)
Automation logic saved as text files. Excel understands these.
Python (Build Step)
Injects .bas modules into Excel and saves as .xlsm. (Windows only; macOS uses manual import.)
Excel (.xlsm)
The final product! A single file with data + macros. Share it, run it, use it at work.
Prerequisites
Click on each item to see the setup guide:
Microsoft Excel
Microsoft 365 or desktop version
Microsoft Excel
Microsoft 365 or desktop versionOpen Excel from your Start menu (Windows) or Applications folder (Mac). If it opens, you're set!
Get Microsoft 365 at microsoft.com/microsoft-365. Free trial available, or use your company/school account.
Open Excel → File → Options → Trust Center → Trust Center Settings → Macro Settings → Select "Disable all macros with notification" (recommended) or "Enable all macros" (only for development, not recommended for daily use).
VS Code with GitHub Copilot
Your AI-powered code editor
VS Code with GitHub Copilot
Your AI-powered code editorGo to code.visualstudio.com and download for your OS. Install with default settings.
Open VS Code → Extensions (Ctrl+Shift+X) → Search "GitHub Copilot" → Click Install.
Click the Copilot icon in the sidebar → Sign in with your GitHub account. Need a subscription? Start a free trial at github.com/features/copilot.
Search "VBA" in Extensions → Install for syntax highlighting in .bas files.
Python 3.10+
Windows recommended for automated build
Python 3.10+
Windows recommended for automated buildGo to python.org/downloads and download Python 3.10 or later.
Run the installer. Important: Check "Add Python to PATH" at the bottom before clicking Install.
Open Terminal/Command Prompt → Type python --version → Should show "Python 3.10.x" or higher.
Run: pip install pywin32. This allows Python to control Excel.
Excel Trust Center
Enable VBA project access
Excel Trust Center
Enable VBA project accessOpen Excel → File → Options (bottom left).
Click "Trust Center" in the left sidebar → Click "Trust Center Settings..." button.
Click "Macro Settings" → Select "Enable all macros" (or at minimum "Disable with notification").
Check ✅ "Trust access to the VBA project object model" → Click OK → Click OK again.
Setup: Prepare Your Workspace
Before we start the automation loop, let's set up your project folder.
1. Create a New Folder & Open in VS Code
Create a new folder anywhere on your computer (e.g., my-excel-automation). Open VS Code → File → Open Folder → Select your new folder.
2. Add Your Excel File (If You Have One)
Simply copy your Excel file (e.g., employee-data.xlsx) into this folder. That's it!
No problem! In the next step, Copilot will create one for you based on your description.
3. Tell Copilot to Create the Project Structure
Open Copilot Chat and describe what you want:
"I have an Excel file called employee-data.xlsx with names in column A, salaries in column B, departments in column C. Create a complete project structure with: macros folder for VBA files, a Python build.py script that injects VBA modules into Excel and saves as .xlsm, and requirements.txt with needed packages."
Save the files Copilot generates. Create the folder structure if it asks you to. Install requirements: pip install -r requirements.txt
The Loop: Generate → Deploy → Test → Iterate
This is where the magic happens. You describe, AI generates, you test, you refine. Repeat until perfect.
Describe: Tell Copilot What You Want
Open Copilot Chat. Describe what automation you need in plain English:
"Create a VBA macro that removes duplicate rows from column A, formats dates in column B to DD/MM/YYYY, and highlights cells with invalid emails in column C in red."
"Create a macro that calculates the total and average of salaries in column B, groups by department in column C, and writes a summary table at the bottom."
Generate: AI Creates VBA + Python
Copilot generates all the code. Save each file:
- VBA macro file (.bas) — save to
macros/folder - Updated build.py if needed
- All code with comments explaining what it does
Tip: Ask Copilot to explain any part of the code if you're curious. You don't need to understand it to use it!
Deploy: Build the .xlsm
Tell Copilot to run the build script. Python injects all VBA macros into your Excel file:
"Run the build script"
macOS users: Open your Excel file → Go to Tools → Macro → Visual Basic Editor → File → Import File... → Select your .bas files. No Python needed.
Test: Run Your Macro in Excel
Open the generated .xlsm file and run your macro:
output.xlsm in ExcelPressing Alt+F8 every time can be tedious. You can ask AI to create a button directly in your spreadsheet — just describe where you want it!
"Add a green button labeled 'Generate Report' in cell E1 that runs the GenerateReport macro when clicked."
"Create two buttons at the top of the sheet: one blue button 'Clean Data' and one orange button 'Export PDF', each running their respective macros."
VBA and Python can both create buttons programmatically. Just tell AI the position, size, color, and label you want — it generates the code. Your users then just click the button to run the automation!
Perfect!
Great! You're done. Share your .xlsm file with your team.
Needs changes?
Go back to step 1. Tell Copilot what to fix or add.
Iteration Examples
Something not right? Just describe the problem to Copilot:
"It only processes the first 100 rows but I have 5000 rows."
"Update the macro to process all rows with data, not just 100."
"I want to also export the results as a PDF."
"Add code to export the active sheet as a PDF after running the macro."
"I want yellow highlighting instead of red for values below 50."
"Change the highlighting color from red to yellow for values below 50."
Describe → AI generates code → Tell Copilot "run the build" → Test in Excel → Repeat until perfect!
Pro tip: Keep Excel open with your .xlsm file during the loop! Each time you run python build.py, it creates a fresh output file. Just close and reopen the file in Excel to see the updated macros.
FAQ & Limitations
Can VBA run inside VS Code?
No. VBA executes only inside Excel. We use VS Code for authoring and Python to inject modules into the workbook.
Do I need to write code manually?
No! You describe tasks in comments; Copilot generates VBA. You review and iterate.
Is Python required?
It simplifies building the final .xlsm on Windows. On macOS, you can import modules manually — no Python needed.
Security considerations?
Enable macros only from trusted sources. Some enterprises require digitally signed macros.
What if I Don't Have an Excel File Yet?
Nothing changes! The workflow is identical. Just describe your data structure and automation needs to AI, and ask it to generate a blank Excel template along with the VBA macros.