-
Solved: Can you instantiate Class Module Object from Another Project?
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"
[vba]
Option Explicit
Public x As Double
Public y As Double
Function sum() As Double
sum = x + y
End Function
[/vba]
This code goes into a standard module on workbook B.
[vba]
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
[/vba]
So basically, I need to know how to reference "SumClass" from workbook B.
-
I found my own answer
http://www.cpearson.com/excel/ClassModules.htm
This is what I needed to do:
In workbook A class module (renamed "SumClass" in properties):
[vba]
Option Explicit
Public x As Double
Public y As Double
Function sum() As Double
sum = x + y
End Function
[/vba]
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:
[vba]
Public Function GetSumClass() As SumClass
Set GetSumClass = New SumClass
End Function
[/vba]
In workbook "B" standard module:
[vba]
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
[/vba]
works like a charm!
Last edited by nitt1995; 07-13-2006 at 10:38 AM.
-
Thanks for posting the solution!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules