RatioXL
Programming inside the workbook RatioXL Pro

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.

You don't need to know how to program in Lua or Python. Describe the calculation in your own words and ask the AI you use for the script.

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
The problem

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.

1

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.

2

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.

3

Everything by hand

VBA has no scientific or charting libraries: numerical calculation is programmed from scratch and figures are left to Excel charts.

Comparison

Everything a macro does, without its drawbacks

AspectVBALua with RatioXL ProPython with RatioXL Pro
Where the code livesA macro module; the workbook becomes .xlsmA module inside the .xlsx, with several files if neededA module of .py files inside the .xlsx
Setup requiredNothingNothing: LuaJIT comes inside RatioXL ProThe computer's Python (3.8 or later) and the libraries it uses
SecurityThe macro has full access to the computerSandboxed: no access to the system or the network; it only reads files from the module or the workbook's folderFull access, but it asks for permission for each version of the code
SpeedInterpretedJust-in-time compiled: 100 million iterations in about 0.4 sThat of numpy and pandas for vectorized calculation
LibrariesThe COM libraries registered on the computermath, string, table and LPeg for reading text formatsThe whole ecosystem: numpy, pandas, matplotlib, seaborn, plotly, ifcopenshell…
When it runsWhen a button is clicked or on an eventWhen the document is prepared and, with eventos.lua, when the workbook opens, when cells change, or when a button is clickedWhen the document is prepared (editor, viewer, PDF, Word…), before the other tags
Keeping results currentNo way to knowIt is reused only if neither the code nor anything it read has changed; if an input changed, it runs again
What goes into the reportThe cells the macro writesSheets, 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 failsAn error dialog stops your workA notice in the document with the message and the script line
Speed

π 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.

For this example, the script in RatioXL is up to 8 times faster than a VBA macro!

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

local N = 10000000 local random = math.random local dentro = 0 for i = 1, N do local x, y = random(), random() if x * x + y * y <= 1 then dentro = dentro + 1 end end return ('π ≈ **%.5f** (%.2f s)'):format(4 * dentro / N, xl.clock())

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

Sub MontecarloPi() Const N As Long = 10000000 Dim i As Long, dentro As Long Dim x As Double, y As Double, t As Double Randomize t = Timer For i = 1 To N x = Rnd: y = Rnd If x * x + y * y <= 1 Then dentro = dentro + 1 Next i Debug.Print 4 * dentro / N, Timer - t ' π and seconds End Sub

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).

How it works

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.

1 · Data

Cells with units

The data stays in Excel: defined names, the Parameters sheet, or tables.

2 · Script

XL.Lua or XL.Py

It reads with xl.param and xl.table, calculates, and writes with xl.write.

3 · Document

XL(…) tags

The document's text, tables, and figures show what the script left behind.

4 · Issue

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

local L = xl.param('Luz') -- m local q = xl.param('Carga_q') -- kN/m local filas = { { 'x (m)', 'M (kN·m)' } } for i = 0, 40 do local x = L * i / 40 filas[#filas + 1] = { x, q * x * (L - x) / 2 } end xl.write('Momentos', filas) return ('Maximum moment: **%.1f kN·m**'):format(q * L^2 / 8)

Python module “Grafico” · main.py

import matplotlib.pyplot as plt df = xl.dataframe('Momentos') fig, ax = plt.subplots(figsize=(6, 3)) ax.plot(df['x (m)'], df['M (kN·m)']) ax.set_xlabel('x (m)') ax.set_ylabel('M (kN·m)') xl.figure('momentos', fig) # leaves momentos.png and .svg in the workbook xl.output('Bending moment diagram of the beam.')

In the document

## Beam moments XL.Lua(Viga) The beam with a span of XL(Luz) and a load of XL(Carga_q) has these moments: XL.Py(Grafico) XL.Img(momentos:14cm)

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.

Which one to choose?

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.memo only 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.

Example workbooks

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.

To be fair

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.

With VBA, the calculation and the report go their separate ways. With RatioXL, the script is part of the report.
If you already have macros

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.

You

Gather the macro

Copy its code from the VBA editor (Alt+F11) and note which cells it reads and which sheets it writes to.

The AI

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.

You

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.

You

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.