Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tactical (read 'hack') solution to avoid having the VBA code and Excel sheet as one binary file?

Tags:

excel

vba

As I understand it when I create an Excel sheet with VBA code the VBA code is saved as binary with the sheet. I therefore can't put the code into source control in a useful way, have multiple devs working on the problems, diffing is difficult, etc.

Is there a way round this without switching to VSTO, COM addins etc? E.g. for the sheet to load all is VBA in at runtime from a web service, shared drive, etc? Any ideas appreciated.

Thanks.

like image 474
ng5000 Avatar asked Feb 12 '09 09:02

ng5000


1 Answers

I wrote a kind of build system for Excel which imports the VBA code from source files (which can then be imported into source control, diffed, etc.). It works by creating a new Excel file which contains the imported code, so it might not work in your case.

The build macro looks like this, I save it in a file called Build.xls:

Sub Build()
    Dim path As String
    path = "excelfiles"

    Dim vbaProject As VBIDE.VBProject
    Set vbaProject = ThisWorkbook.VBProject

    ChDir "C:\Excel"
    ' Below are the files that are imported
    vbaProject.VBComponents.Import (path & "\something.frm")
    vbaProject.VBComponents.Import (path & "\somethingelse.frm")

    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs "Output.xls"
    Application.DisplayAlerts = True

    Application.Quit
End Sub

Now, the VBIDE stuff means you have to import a reference called “Microsoft Visual Basic for Applications Extensibility 5.3", I think.

Of course you still have the problem with having to start Excel to build. This can be fixed with a small VB script:

currentPath = CreateObject("Scripting.FileSystemObject") _
    .GetAbsolutePathName(".")
filePath = currentPath & "\" & "Build.xls"

Dim objXL
Set objXL = CreateObject("Excel.Application")
With objXL
    .Workbooks.Open(filePath)
    .Application.Run "Build.Build"
End With
Set objXL = Nothing

Running the above script should start the build Excel file which outputs the resulting sheet. You problably have to change some things to make it movable in the file system. Hope this helps!

like image 142
Jonas Avatar answered Sep 24 '22 22:09

Jonas