Can an object variable survive between calls of multiple subs in a single class module?

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
Here's the problem. I'm trying to get a checkbox created at runtime to adda new page to a multipage object, which is also created at runtime.

The object variable associated with the multipage object won't survive between calls of two separate subs in the class module I'm using. It appears as nothing in a later sub despite being set in an earlier sub.

This example uses a blank userform called "userform2" and a class module called "class2":

Userform2 object module code:

Code:
Public Mult, mp As Object
Private checker2, mp1 As Class2
Private Sub UserForm_Initialize()
    Dim checker1 As MSForms.CheckBox
    Set checker1 = Me.Controls.Add("forms.Checkbox.1")
    With checker1
        .Height = 22
        .Left = 6
        .Top = 12
        .Width = 72
        .Name = "CheckBox1"
        .Caption = "CheckBox1"
    End With
    Set checker2 = New Class2
    checker2.Init checker1
 
    Set mp = Me.Controls.Add("forms.Multipage.1")
    With mp
        .Height = 40
        .Left = 6
        .Top = 40
        .Width = 100
        .Pages.Remove (1)
    End With
    Set mp1 = New Class2
    mp1.Init2 mp
End Sub

Class2 module code:
Code:
Public Mult As Object
Private WithEvents cb As MSForms.CheckBox
Public Sub Init(ByVal CBox As MSForms.CheckBox)
    Set cb = CBox
End Sub
Public Static Sub Init2(ByVal Multi As MSForms.MultiPage)
    Set Mult = Multi
    If Not IsEmpty(Mult) Then: MsgBox Mult.Name
End Sub
Public Sub cb_Click()
    If cb.Value = True Then
        If Not Mult Is Nothing Then: MsgBox Mult.Name
    End If
End Sub

Please help me out with this.
 

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.
If I understand right, the peoblem is that you are creating 2 instances of Class2 but the Mult Object variable in your Class module is only initialised in the first Class instance hence not being seen in the second Class instance.

One solution is to amend the userform code as follows ( Edit in red)

Code:
Public Mult, mp As Object
Private checker2, mp1 As Class2
Private Sub UserForm_Initialize()
    Dim checker1 As MSForms.CheckBox
    Set checker1 = Me.Controls.Add("forms.Checkbox.1")
    With checker1
        .Height = 22
        .Left = 6
        .Top = 12
        .Width = 72
        .Name = "CheckBox1"
        .Caption = "CheckBox1"
    End With
    Set checker2 = New Class2
    checker2.Init checker1
 
    Set mp = Me.Controls.Add("forms.Multipage.1")
    With mp
        .Height = 40
        .Left = 6
        .Top = 40
        .Width = 100
        .Pages.Remove (1)
    End With
[COLOR=Red][B]'    Set mp1 = New Class2[/B][/COLOR]
    [B][COLOR=Red]checker2.Init2 mp[/COLOR][/B]
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,519
Members
448,968
Latest member
Ajax40

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