Call a class module from a userform?

excel-yes

New Member
Joined
May 10, 2011
Messages
9
I am trying to learn how to get classes and collections to work together with userforms. How can I call a class module's stuff from a userform module? The userform calls a macro from a different regular module successfully.

The class module is public not private. The userform runs successfully except when the button is clicked that should call the class module. Then I get an error 424 object required msg. Or if I change the problem line I get a sub or function not defined error msg. for the same line. The line that says "Class1.whatever" below is the line that is highlighted to be debugged. Also I have gotten error messages for the line below it.

I put this in the userform module:
Code:
Private Sub blah_Click()
Class1.whatever
Range("A1").Value = Class1.stuff.Item(1)
End Sub

I put this in the "Class1" class module:
Code:
Sub whatever()
Dim stuff As Collection
Set stuff = New Collection
stuff.Add "First thing in collection"
End Sub
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Your class must have a means of accessing its collection:

Code:
Private Stuff As Collection
Sub whatever()
Dim stuff As Collection
Set stuff = New Collection
stuff.Add "First thing in collection"
End Sub
'------------------------------------------------------------
Public Function GetStuff(ByVal arg As Long) As Variant
    If Stuff Is Not Nothing Then
        GetStuff = Stuff.Item(arg)
    End If
End Function

Also you must create the class object before you can use it:
Code:
Private Sub blah_Click()
Dim x As MyCustomClass
Set x = New MyCustomClass
x.whatever
MsgBox x.GetStuff(1)
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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