Excel :: Global Class Instance

Seorch

New Member
Joined
May 4, 2012
Messages
3
Hi,

I’m new to VBA and especially how to use it in an OO way. My intention is to create a base class "myClass" with common methods to use within my Excel application. These methods should be available in userForms and other modules (global scope).

Don’t know if it’s possible, but I would prefer to insatiate the class on "Workbook_Open" and then use the instance in other modules.

Example

myClass (Class Module)
Code:
Public Sub Alert(Value As String)
   MsgBox Value
End Sub

ThisWorkbook

Code:
Dim My As myClass

Public Sub Workbook_Open()
   Set My = New myClass
End Sub

Module1

Code:
Sub Test()
   My.Alert "Message"
End Sub

Is there a way of achieving this? The reason is that I don’t want to instantiate the class over and over again.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Not 100% sure what you are trying to do but for this example try declaring My as Public in a standard module.
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,594
Members
452,655
Latest member
goranzoric

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top