Note that there are some explanatory texts on larger screens.

plurals
  1. POSource control of Excel VBA code modules
    primarykey
    data
    text
    <p>I'd like to be able to source control my Excel spreadsheet's VBA modules (currently using Excel 2003 SP3) so that I can share and manage the code used by a bunch of different spreadsheets - and therefore I'd like to re-load them from files when the spreadsheet is opened.</p> <p>I've got a module called Loader.bas, that I use to do most of the donkey work (loading and unloading any other modules that are required) - and I'd like to be able to load it up from a file as soon as the spreadsheet is opened.</p> <p>I've attached the following code to the Workbook_Open event (in the ThisWorkbook class).</p> <pre><code>Private Sub Workbook_Open() Call RemoveLoader Call LoadLoader End Sub </code></pre> <p>Where RemoveLoader (also within the ThisWorkbook class) contains the following code:</p> <pre><code>Private Sub RemoveLoader() Dim y As Integer Dim OldModules, NumModules As Integer Dim CompName As String With ThisWorkbook.VBProject NumModules = ThisWorkbook.VBProject.VBComponents.Count y = 1 While y &lt;= NumModules If .VBComponents.Item(y).Type = 1 Then CompName = .VBComponents.Item(y).Name If VBA.Strings.InStr(CompName, "Loader") &gt; 0 Then OldModules = ThisWorkbook.VBProject.VBComponents.Count .VBComponents.Remove .VBComponents(CompName) NumModules = ThisWorkbook.VBProject.VBComponents.Count If OldModules - NumModules = 1 Then y = 1 Else MsgBox ("Failed to remove " &amp; CompName &amp; " module from VBA project") End If End If End If y = y + 1 Wend End With End Sub </code></pre> <p>Which is probably a bit overcomplicated and slightly crude - but I'm trying everything I can find to get it to load the external module!</p> <p>Often, when I open the spreadsheet, the RemoveLoader function finds that there's a "Loader1" module already included in the VBA project that it is unable to remove, and it also fails to load the new Loader module from the file.</p> <p>Any ideas if what I'm trying to do is possible? Excel seems very fond of appending a 1 to these module names - either when loading or removing (I'm not sure which).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload