Question

I have 2 distinct VBA projects that make use of 8 identical classes. If i export those classes to a directory in my disk, is there a way to instantiate them programatically to avoid code duplication?

I don't see any problem by having the same code in 2 distinct projects, but my boss asked if it is possible and so far i haven't found anything that could lead me to an answer in the microsoft online help and in some books i've got.

Was it helpful?

Solution 3

As a workaround I exported the methods in those classes to a DLL file and saved it in the server. Whenever the user opens the .xlsm file, a method is called to register the DLL in their computers and then I can use the methods and properties contained in it without further problems.

I also considered the other answers to this question. But sending 2 files (one that contains the datasheet that will be used and another one for the library) would not be a good idea (users would not understand what is the reason for that) because those programs will be used by a lot of users.

OTHER TIPS

Now that I understand your question, a central library of sorts may be your best option, there are others but I'm always a fan of libraries.

To achieve this:

  • Create a new workbook, import all of your class modules in to the VBA project in that workbook. Name your project something like MyCentralLibrary (something different than VBAProject)
  • Set the class module's Instancing Property to PublicNotCreatable (change this in the properties window)
  • Now here comes the slightly stupid part (I never was a fan of VBA). Projects referencing this library will be able to see all the classes but never instantiate a new instance of them. So what you'll have to do is for each class you put in the library, write a function with the class as the return type, like so:

    Public Function GetMyClass() As MyClass
        Set GetMyClass = New MyClass
    End Function
    
  • Now in the project you want to reference your central library. Go to Tools -> References and choose your library name from the list. Now you can instantiate classes from that library in your project like so:

    Public c As MyCentralLibrary.MyClass
    Set c = MyCentralLibrary.GetMyClass()
    

Again, there's other ways of doing it, but I'm always a proponent of libraries (at least in other languages)

As a note, if/when you move between computers,, you'll obviously have to move the workbook with your library in it around and re-do the references for any projects referencing the library on the new computer.

My two cents, if this were to scale more I'd go ahead and grab the free version of Visual Studio and turn this into a class library that can be referenced from Excel. But that's just me and my dislike of VBA shining through ;).

Yes, it is possible, but you have to change some security settings. I do something similar to this in all my VBA projects so I can use source control.

The code to import a class module into your project would be

ThisWorkbook.VBProject.VBComponents.Import "C:\path\to\myclass.cls"

In order for this to work, you have to change your security settings to allow access to the VBA project object model. This is because in the past some viruses has spread via this mechanism. The setting is in File > Options > Trust Center > Trust Center Settings... > Macro Settings > Trust access to the VBA project object model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top