Log in

View Full Version : Solved: Can you instantiate Class Module Object from Another Project?



nitt1995
07-13-2006, 08:27 AM
I have set up a class module in an add in which contains the blue print of a set of variables and methods to act on those variables.

I would like to instatiate this object from other projects. Is this possible? I have added the reference to the Add-in from Tools->References but cannot DIM a new object to this class module.

The following code works when both modules are in the same worksheet, but not in different workbooks. I know that it shouldn't work unless I can properly reference the class module of workbook A to the module on workbook B, but I can't figure out the syntax.

This code goes into a class module on workbook A. Call this class module "SumClass"

Option Explicit
Public x As Double
Public y As Double
Function sum() As Double
sum = x + y
End Function


This code goes into a standard module on workbook B.

Option Explicit
Dim test As SumClass
Sub main()
Set test = New SumClass
test.x = 3
test.y = 4
MsgBox Prompt:=test.sum 'Should output 7
End Sub


So basically, I need to know how to reference "SumClass" from workbook B.

nitt1995
07-13-2006, 10:14 AM
http://www.cpearson.com/excel/ClassModules.htm

This is what I needed to do:

In workbook A class module (renamed "SumClass" in properties):

Option Explicit
Public x As Double
Public y As Double
Function sum() As Double
sum = x + y
End Function


make sure that it is renamed to something ("SumClass" in this case) in the properties window.

Also, under Instancing (in properties window) change from 1(private) to 2(publicNotCreatable).


In workbook "A" standard module:

Public Function GetSumClass() As SumClass
Set GetSumClass = New SumClass
End Function


In workbook "B" standard module:

Option Explicit
Dim test As A.SumClass
Sub main()
Set test = A.GetSumClass
test.x = 3
test.y = 4
MsgBox Prompt:=test.sum 'Should output 7
End Sub


works like a charm!

Zack Barresse
07-13-2006, 10:30 AM
Thanks for posting the solution! :yes