Thread: Class Module
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Class Module

You can't directly share a class module between projects. You
can, however, have a public function in the workbook that
contains the class that returns as its result a new instance of
the class. For example, suppose WB1.xls has a project name of
MyProj and a class named CMyClass.

Then, set a reference from WB2.xls to WB1.xls (in VBA go to the
Tool menu, choose References, and check MyProj). Then in a code
module in WB1.xls, create a function

Public Function GetClass As CMyClass
Set GetClass = New CMyClasss
End Function

In WB2.xls, instantiate the class with code like

Public Sub AAA()
Dim C As MyProj.CMyClass
Set C = MyProj.GetClass
' more code
End Sub

Ensure that the Instancing Property of the Class is
PublicNotCreatable, not Private.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Bill Martin" wrote in message
...
How does one share a class module among XLS files? I can share
macros for
example by storing them in Personal.xls or by creating a
VBAProject reference to
the file which contains the code. I presume there's a way to
share the code in
a class module short of replicating the module it in every xls
file?

I've tried putting the code in a class module contained within
a Library.xls
file that's already referenced but when I try to use the class
code I get an
error that the user-defined type is not defined. It works
though if I replicate
the class module in every XLS file that wants to use it.

Plainly I'm missing something.

Thanks...

Bill