Using Class Modules in Userforms

svjack9

New Member
Joined
Feb 9, 2014
Messages
32
This is a general question regarding when a class has to be instantiated in Excel VBA. If I create a class object (let's call it oSession) in Userform_Initialize() can I use that instance of the class in other form procedures?
For example:
UserForm_initialize()
Dim oSession as New Session
with oSession
.Property1=Data1
.Property2=Data2
End with
End Sub

Does oSession go out of scope at "End Sub" or can I pass oSession to other procedures used by the form? My guess is that it goes out of scope, but I'm hoping there is a way to do it.

Hope I've used the right terminology, but, if not, I'm open to learning!

Svjack9
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Yes. oSession will go out of scope at "End Sub"

In order to preserve the class instance,you can pass it as an argument to other procedures used by the form but within the initialize event sub. (ie: before reaching "End Sub")

You can aslo add it to a collection but the collection must be declared at the top of the userform module.

Why don't you simply declare oSession at the top of the userform module ?
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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