Lua and Python in the workbook, instead of VBA
Some calculations don't fit in cells: iterations, loops over thousands of elements, reading an IFC file, drawing twenty charts. The usual approach is to write them in a VBA macro. With RatioXL Pro that code is written in Lua or Python, lives inside the .xlsx itself, and its result goes straight into the calculation report.
Lua and Python scripts and Excel events in Lua are part of RatioXL Pro, the desktop edition: a web add-in, like RatioXL from the Office store, cannot run LuaJIT or your computer's Python. See both editions.
- No programming skills needed
- No macros, no .xlsm
- LuaJIT included
- numpy, pandas, matplotlib
- Result tied to the report
VBA solves the calculation, but leaves the report on its own
VBA still works, but it carries problems that weigh more and more in a calculation report.
Blocked macros
A workbook with macros has to be .xlsm, and Office blocks macros by default in files that arrive by email or from the internet. The reviewer gets a security warning instead of the calculation.
No one knows if it's up to date
The macro runs when someone clicks the button. If an input changes afterward, the cells it writes fall out of sync and nothing warns you: the report may go out with results from the previous version.
Everything by hand
VBA has no scientific or charting libraries: numerical calculation is programmed from scratch and figures are left to Excel charts.
Everything a macro does, without its drawbacks
| Aspect | VBA | Lua with RatioXL Pro | Python with RatioXL Pro |
|---|---|---|---|
| Where the code lives | A macro module; the workbook becomes .xlsm | A module inside the .xlsx, with several files if needed | A module of .py files inside the .xlsx |
| Setup required | Nothing | Nothing: LuaJIT comes inside RatioXL Pro | The computer's Python (3.8 or later) and the libraries it uses |
| Security | The macro has full access to the computer | Sandboxed: no access to the system or the network; it only reads files from the module or the workbook's folder | Full access, but it asks for permission for each version of the code |
| Speed | Interpreted | Just-in-time compiled: 100 million iterations in about 0.4 s | That of numpy and pandas for vectorized calculation |
| Libraries | The COM libraries registered on the computer | math, string, table and LPeg for reading text formats | The whole ecosystem: numpy, pandas, matplotlib, seaborn, plotly, ifcopenshell… |
| When it runs | When a button is clicked or on an event | When the document is prepared and, with eventos.lua, when the workbook opens, when cells change, or when a button is clicked | When the document is prepared (editor, viewer, PDF, Word…), before the other tags |
| Keeping results current | No way to know | It is reused only if neither the code nor anything it read has changed; if an input changed, it runs again | |
| What goes into the report | The cells the macro writes | Sheets, Markdown or Typst text in place of the tag, SVG and PNG figures with XL.Img, and interactive HTML pages saved in the workbook | |
| If it fails | An error dialog stops your work | A notice in the document with the message and the script line | |
π with 10 million points in 0.35 seconds
We benchmarked a classic Monte Carlo estimate of π: sampling random points in a unit square and counting those inside the unit quarter-circle. The hit ratio converges to π/4, yielding three to four accurate decimal places at 10 million points.
Measured on a Windows 11 PC; each time is the best of several runs. The Lua script runs on the same LuaJIT that ships with the add-in, and the VBA macro in Excel. Python without numpy runs the same loop and numpy does it with vectors; its time does not include starting Python or loading the library.
Lua module “Pi” · main.lua
The loop gets compiled
LuaJIT translates numerical loops into machine code as they run. That's why the same loop, written the same way, takes 8 times longer as a VBA macro and 11 times longer in Python without numpy, which interpret it.
The same calculation in VBA
To time it on your computer: in Excel, press Alt+F11, choose Insert → Module, paste the macro, and run it with F5. The result appears in the Immediate window (Ctrl+G).
The script is part of the document
A script isn't launched by hand. The document calls it with a tag on its own line, and whatever the script writes is already in the sheets when the rest of the document reads it.
Cells with units
The data stays in Excel: defined names, the Parameters sheet, or tables.
XL.Lua or XL.Py
It reads with xl.param and xl.table, calculates, and writes with xl.write.
XL(…) tags
The document's text, tables, and figures show what the script left behind.
PDF with a fingerprint
The issued PDF carries a fingerprint of the workbook's state, so you can check whether it is still up to date.
Lua module “Viga” · main.lua
Python module “Grafico” · main.py
In the document
The PDF uses the figure's SVG (vector) and Word and PowerPoint use the PNG. If you change the span in Excel, the viewer runs both modules again and redraws the figure; if nothing changes, it reuses the previous result without starting Python.
Lua for calculating, Python for what already exists
Choose Lua when…
- The workbook goes to other people and you don't want them to have to install anything.
- The calculation is your own: iterations, searches, numerical integration, thousands of elements.
- You need to read output from other programs (node listings, sensor logs) with LPeg grammars.
- You prefer a sandbox: the script cannot write to disk or use the network.
Choose Python when…
- What you need is already done in a library: statistics, pandas, ifcopenshell.
- You want publication-quality charts with matplotlib, seaborn, or plotly.
- The expensive steps rarely repeat:
xl.memoonly redoes the figure whose data changed. - The computers that open the workbook have Python, or the saved result is enough for them.
When you close the workbook, RatioXL offers to save each module's result in it. Whoever opens it later sees the complete report without running anything, as long as the data and the code stay the same.
Real cases included in the download
P-M interaction of a column
Lua calculates the interaction diagram, returns Typst figures for the PDF, and leaves a 3D HTML viewer you can rotate with the mouse, inside the workbook.
Spillway
The iterative hydraulic calculation lives in a Lua script in the workbook; the sheets it writes feed the calculation report.
Quantity takeoffs from IFC
The same BIM model read with Lua and with Python (ifcopenshell), with the volume and the time of each compared in a sheet of the workbook.
Charts with Python
21 engineering figures with Matplotlib, Seaborn, and Plotly, and an interactive gallery that opens in the HTML Viewer.
When VBA is still the right tool
RatioXL scripts run when the document is prepared: they read data from the workbook, calculate, and leave sheets, figures, and text. Lua scripts can also respond to workbook events through an eventos.lua file: when it opens, when cells in a range change, and when a shape or button is clicked. What they don't do is drive Excel's object model (open other workbooks, move files, apply formatting, change menus, or show their own dialog boxes) or respond to other events, such as a selection or a save. For that, VBA is still the way. For calculating and carrying the result into a report that gets signed, Lua and Python inside the workbook do the job better.
Hand your VBA macros to the AI
If you use spreadsheets today that calculate with VBA macros, you don't need to rewrite them by hand. Ask the AI to replicate each macro as a RatioXL Lua or Python script: the calculation stays inside the workbook, tied to the report, and with no macros. The "ratioxl" Skill already knows the tags and the xl.* functions.
If you don't have macros, you don't need to know Lua or Python either: tell the AI which data in the workbook it should read, what calculation it should do, and where to leave the result, and it writes the script. You just check that the results are correct.
Gather the macro
Copy its code from the VBA editor (Alt+F11) and note which cells it reads and which sheets it writes to.
Writes the script
Ask it for a Lua or Python module that reads the data with xl.param and xl.table and leaves the results with xl.write.
Save it in the workbook
Save the script's folder with Modules → Save module in workbook… and call it from the document with XL.Lua or XL.Py.
Compare before deleting
With the same data, check that the script gives the same results as the macro. Once they match, save the workbook as .xlsx, without macros.
The AI translates quickly, but it can make mistakes: that's why the last step is never skipped. If a macro shows its own forms or responds to events other than opening the workbook, changing cells, or clicking a button, that part stays in VBA.
Move your next macro to Lua or Python
Download the trial: it comes with LuaJIT included and the example workbooks with their scripts to get you started. The AI can write new scripts for you.