7

I have written an XLA add-in for Excel. This add in includes a class module. I was wondering how I can expose this class module for use. I have searched around and found a way to do it with Visual Studio, but sadly I do not have visual studio. Is there any way to do this just from XLA?

1 Answer 1

8

You can make a public function in the XLA that returns an instance of the class. Then any workbook that has a reference to the XLA can call that function. Assume you have Book2 which contains Class1 and Book3 where you want to use Class1. Change the Class1 Instancing property to Public Not Creatable (F4 while in the class module).

In Book2 in a standard module create a function

Public Function InstantiateClass1() As Class1

    Set InstantiateClass1 = New Class1

End Function

In Book3, call the function

Public Sub DoStuff()

    Dim clsClass1 As Book2Project.Class1

    Set clsClass1 = instantiateclass1

    clsClass1.prop = "something"

    Debug.Print clsClass1.prop

End Sub

Class1 has one property called prop in this example. Book2's VBProject name was changed to Book2Project.

1
  • +1 on the solution. I like to encapsulate all instance creation functions in a factory Class Module (which would be created in the Book2 XLA in the example, with a Public Not Creatable setting). Then you need only a single creation method in the standard module to create the factory instance, which would be used by the client. This also allows the client to view all the available instantiation methods in the factory object using Intellisense. Jun 19, 2017 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.