- Big data and data engineering
- Process automation
Medium-voltage grid planning: from Excel to Python
Planning the medium-voltage power grid depended on a chain of 14 macro-driven Excel workbooks that were slow and hard to maintain. I am rewriting them in Python and checking that they give exactly the same results, so the process becomes faster, verifiable and easy to improve.
Migration to Python of 14 chained .xlsm workbooks (over 9,500 lines of VBA in the load-transfer engine alone), with a 1:1 replica and cell-by-cell equivalence tests; Steps 0–8 validated (0 differences, also when chained), load-transfer engine replicated against the real macro, and SAIDI, SAIFI and ENS indicators reimplemented.
- My role
- Author: macro analysis, migration, validation and documentation
- When
- 2026–present · Luz del Sur
- Tools
- Python, VBA, Excel, Equivalence tests
- Result
- 14 "Steps" in the chain, from the grid export to load transfers, being migrated in stages into a single program
The story
01 / 04
The problem
To decide how the medium-voltage power grid grows and is operated, the team used a chain of 14 Excel workbooks with macros.
MV planning in Steps 0–13: chained .xlsm workbooks from the grid export to load transfers and the quality-of-supply matrix.
02 / 04
Why it is hard
It was slow, hard to maintain and almost impossible to review: if something failed in one workbook, everything after it was in doubt.
Over 9,500 lines of VBA across 127 sheets in the load-transfer engine alone; logic spread across macros, formulas and implicit dependencies between workbooks.
03 / 04
The key idea
Rewrite each step in Python so it does exactly the same as the Excel, and check it automatically, cell by cell.
Automatic extraction of VBA, formulas and structure; 1:1 replica per Step; equivalence tests against the Excel outputs.
04 / 04
The result
The first nine steps and the load-transfer macros already give the same results as the Excel. On that base the improvements started: in one test, the improved version of the cascading transfers found 43 possible transfers where the original macro found 18. The chain already ran with October's data without touching the program. In the end it will be a single program: faster, verifiable and easy to improve.
Steps 0–4 and 5–8 validated cell by cell (Differences = 0 in workbook mode and in --chain mode, with strict chained validators); transfer macros replicated and validated against the real macro (36/36; version with isolability 176/176 rows, 0 differences; cascade 89/89 cells); five versions of the peak-demand workbook (off-peak, peak-hour and maximum-demand scenarios) replicated with 0 differences across their 7 sheets; first cascade improvement: 43 transfers versus 18 in the replica, with 100 switching operations; the 0–8 chain ran with the October power flow without code changes; SAIDI, SAIFI and ENS reimplemented with improvement simulation; improvements sit behind configuration flags.
What's next
In progress: keep migrating the remaining Steps until the whole chain is a single program.
Impact
- 14"Steps" in the chain, from the grid export to load transfers, being migrated in stages into a single program
- 0–8Steps replicated and validated cell by cell against the Excel results, also running as a chain
- 9,500+lines of VBA across 127 sheets documented for the load-transfer engine
- Quality-of-supply indicators (SAIDI, SAIFI, ENS) reimplemented with improvement simulation
How it works
- Extract and understand. Scripts that extract the VBA code, formulas and structure of each workbook, plus detailed documentation of what each macro does.
- Replicate 1:1. Each Step is rewritten in Python to do exactly the same as the Excel.
- Validate. The Python and Excel results are compared automatically, cell by cell.
- Unify and improve. The Steps are joined into a single program. Improvements are implemented switched off by configuration so they do not break equivalence.
Technical details
- Covers line sections, double circuits, MV/MV transformers, auxiliaries and routes, transferability, auxiliary calculations, load transfers (which feeder to transfer to and with which switching operations) and the quality-of-supply matrix (SAIDI, SAIFI, ENS and compensation).