Scriptify VBA by Import and Export VBA Projects
TIP
As a major pain point for VBA programers and modelers, the VBA code base is always bundled together with an Excel file which is essentially a zipped package with a bunch of xml's. Unfortunately, the code itself is compiled into a binary file and is impossible to manage directly as the source code. This project explores ways to import and export the VBA code to/from the excel file so that it can be better maintained.
The Motivation of the project
As a long time user and 'developer' of VBA, I eventually realized that VBA is not really a programming language although it shares a lot of things with modern .NET language. However, no matter how quirky and difficult to cope with it, it has always been playing an important role in my jobs. As an actuary, we have heavy models built in Excel and almost every single one of them was powered with VBA. For those who are not familiar with actuarial work:
- It requires high accuracy of calculation and extreme effective production process in the same time;
- The work is performed by a bunch of so called actuaries who had spent enormous of their time in professional training and exams but none of those were related with computer science or programming in any remote sense.
- The work requires high performance models and is usually the bottle neck for those insurance giants with trillions of assets and liabilities on their book.
- In almost every those corporations, we don't have access to those cutting-edge technologies and fancy tools to build our models.And we usually don't have enough resources outside our small group to help us build those models. Overtime, some extremely intelligent folks started to build the models with spreadsheets and VBA programs. In decades, it does a fairly decent job in terms of getting the job done. However, those excel/VBA models became so hard to maintain and they eventually became trash.
I wish we could have a better way to maintain our VBA projects just as the real programmers maintaining their projects. Ideally I want my code to be hosted in a repository and can be maintained and developed by a group of people. And the coding changes can be tracked and reviewed just like any other real programming coding. And last but not the least, I want a more powerful IDE to write the VBA coding with auto completion and syntax formatters.
The good news is that although VBA has been invented many years ago and obviously MS has given up on it ever since, it does have a close relationship with the modern VB.NET language. As long as we can somehow decouple the VBA code with the Excel, I will be happy enough. I started this project aiming to have a robust way to import and export the code to/from an excel file.
Why Powershell
After my initial research, it seems there is no way you can do anything about the VBA coding inside the excel file without installing the Excel program (good job MS). This means you will always have to do the development work on a windows machine (Although Excel has MacOS version but it is a totally different animal). And I believe 99% of the actuaries in this world work on a windows machine.
I am aware there are ways in VBA itself that you can actually separate your coding out in plain text files. But I hate the idea that I will have to use some VBA "master" program to control my other VBA programs. The Powershell seems to be the only other tool I could use at my hand.
After some internet search, I came across a well-written snippet from Atif Aziz. Inspired by his brilliant work, I managed to accomplish what I wanted to do with this project.
The Basic Structure
Once I was able to crack down the excel file with Powershell I was able to "peel the onion" and get a peek of the underlying objects and locate exactly where those VBA coding was hidden. Below is a simple demo of it.
|-- Workbook
|-- VBProject
|-- VBComponents[1]
|-- VBComponents[2]
|-- VBComponents[3]
|-- Name: Name of the component, such as sheet1, module1, form1, ect
|-- Type: Type of the component, integers respresenting things such as worksheets, modules, classes, etc
|-- CodeModule: The object that really hosts the coding.
|-- CountOfLines: number of lines this code block has
|-- Lines[1]: string of the first line of code
|-- Lines[2]: string of the second line of code
|-- Lines[3]: string of the third line of code
|-- ...
Import / Export Implementation
While it is totally possible to just copy and paste out line by line for the "CodeModule" object, it will be easier to just import and export the entire VBComponent object. Luckily it is possible to do with a few catches:
When importing, you can't ask Excel to just "overwrite" the existing VBComponent object. Excel will silently create a new VBComponents with a revised name (appending "1" after its original name). So we will need to remove the existing module before we importing from external files.
For certain type of VBComponent, it seems Excel will assign some machine-generated IDs to it and prevent from direct importing from an external file. These include worksheet and userforms. This can definitely be tacked by our "line-by-line" approach but I will keep looking into it to see if there is a better solution.
With vanilla import and export, there will some compile code added by Excel. Below is an example for the classes. But I guess that's good enough as long as we keep in mind and leave it as is.
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' Above is generated automatically by Excel
Sub test()
MsgBox "test test"
End Sub'
For the complete code, you can find it in my Github repo.
Python Implementation
As long as we are clear about the excel file structure and the fact it is "impossible" to accomplish the job without Excel installed, we can implement the import and export in almost any other programming language.
For example, with the help of the win32com library, Python can pretty much do the same thing. A piece of coding with the export functionality (and just for fun a simple GUI) can be found in the same repo.
Final Thoughts
In my future work, I wish we could eventually get rid of the VBA programs and models and have a more powerful coding platform to build our complex models and tools. But for now I think it will be a better practice to separate your coding from your spreadsheet and maintain them in repositories.