Dynamic CheckBox > OnChange Event

Jonas Offersen

New Member
Joined
Feb 13, 2018
Messages
14
Good Morning Everyone

I am trying to make a dynamically created CheckBox show a UserForm OnChange.
I am using a Class module for this, but I keep getting the "object required" error...

Is this something you can help me out with? (pardon this wall of code)


The class I'm using looks like this:
Public WithEvents CBox As MSForms.checkbox
Public frm As UserForm
Dim iForm As UserForm

Private Sub CBox_Change()
iForm = FormActOptions
iForm.Show
End Sub


I declare a collection outside the sub.
Dim mColCBox As New Collection


Part of the Code generating the first userform:
Dim cBoxEvent As ClassCBox

Exist = rCell.Offset(, 8) 'it exist now
Set iCheck = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & CStr(i), True)
Set acmd = Me.Controls.Add("Forms.TextBox.1", "TextBox_" & CStr(i), True)
Set iFormidler = Me.Controls.Add("Forms.Textbox.1", "CountBox_" & CStr(i), True)

With iCheck 'create a dynamic checkbox
.Top = top_ + 18
.Left = FormIActivities.Label1.Left
.Caption = rCell.Value
.Width = FormIActivities.Label1.Width
If (rCell.Value = "Rabat") Then
.Font.Bold = True
End If
End With

Set cBoxEvent = New ClassCBox
Set cBoxEvent.CBox = iCheck
Set cBoxEvent.frm = Me

mColCBox.Add cBoxEvent

OBS! To avoid crowding this place with code, I chose to cut out only what I think is needed. If you want the full code of this sub, do give me a headsup!



Thank you again for trying to help me out!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I am sorry, I was looking for them before, but I failed to find the code tags. xD

Sorry, the line is
Code:
mColCBox.Add cBoxEvent
Code:
 
Upvote 0
The class I'm using looks like this:
Code:
Public WithEvents CBox As MSForms.checkbox
Public frm As UserForm
Dim iForm As UserForm

Private Sub CBox_Change()
    iForm = FormActOptions
    iForm.Show
End Sub

I declare a collection outside the sub.
Code:
Dim mColCBox As New Collection

Part of the Code generating the first userform:
Code:
Dim cBoxEvent As ClassCBox
                    
                    Exist = rCell.Offset(, 8) 'it exist now
                    Set iCheck = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & CStr(i), True)
                    Set acmd = Me.Controls.Add("Forms.TextBox.1", "TextBox_" & CStr(i), True)
                    Set iFormidler = Me.Controls.Add("Forms.Textbox.1", "CountBox_" & CStr(i), True)
                    
                    With iCheck 'create a dynamic checkbox
                        .Top = top_ + 18
                        .Left = FormIActivities.Label1.Left
                        .Caption = rCell.Value
                        .Width = FormIActivities.Label1.Width
                        If (rCell.Value = "Rabat") Then
                            .Font.Bold = True
                        End If
                    End With
                    
                    Set cBoxEvent = New ClassCBox
                    Set cBoxEvent.CBox = iCheck
                    Set cBoxEvent.frm = Me
                    
                    mColCBox.Add cBoxEvent
 
Upvote 0
Before you can add to a collection you must initialize it:
Code:
Set mColCBox = New Collection
 
Upvote 0
We prefer if you use the reply button rather than the reply with quote.
You must move the Set ... as New Collection to a place where it is only called once during the init of the form. Setting that variable to a new collection zaps the current items of the collection so only the last item is added so to speak.
 
Upvote 0

Forum statistics

Threads
1,222,835
Messages
6,168,537
Members
452,196
Latest member
rkb1510

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