Excel VBA events only working on last element of Collection

Joao Inacio

New Member
Joined
Jan 28, 2019
Messages
5
Hello Everyone,

I have a Multipage that is generating new pages with various CheckBox, ComboBox, TextBox and Button objects.

Currently I'm just focusing on the CheckBox objects but the same code will applied for every different objects.
I know that all 5 CheckBox objects are added to the collection but the issue is when I click on the CheckBox only the last object in the collection triggers events, the other 4 don't do anything. I require all objects to be able to be triggered, I know that all is working as expected but I cannot see what I'm doing wrong and for sure it's obvious.

This code is associated with the Multipage to add each object to the collection
Code:
Public ufEventsDisabled As Boolean
Dim cvCheckBoxs As Collection
Dim cCheckBox As EventCheckBox
Dim varControl As Object
Dim ChckBox As Object

Private Sub CommandButton1_Click()

Set cvCheckBoxs = New Collection
Set cCheckBox = New EventCheckBox

For Each varControl In Me.Controls

If TypeName(varControl) = "CheckBox" Then
Set cCheckBox.cvCheckBox = varControl
cvCheckBoxs.Add varControl

End If
Next varControl

Set cvCheckBox = Nothing

End Sub

This is the code of the class event
Code:
Public WithEvents cvCheckBox As MSForms.CheckBox
Public WithEvents cvComboBox As MSForms.ComboBox

Property Get ParentUF() As Object

Set ParentUF = cvCheckBox.Parent
On Error Resume Next
Do
Set ParentUF = ParentUF.Parent
Loop Until Err
On Error GoTo 0
End Property

Private Sub cvCheckBox_Change()

Dim myUF As Object
Set myUF = Me.ParentUF
Dim ChckBox As MSForms.Control
Dim CombBox As MSForms.Control

For Each ChckBox In myUF.Controls
If TypeName(ChckBox) = "CheckBox" Then

MsgBox "changed"

End If
Next ChckBox

End Sub

Private Sub cvCheckBox_Click()

MsgBox "click"

End Sub

Both cvCheckBox_Click and cvCheckBox_Change only occur when the last element of the collection cvCheckBoxs is clicked or changed.

Apreciated for all the help given.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try...

Code:
Public ufEventsDisabled As Boolean
Dim cvCheckBoxs As Collection
Dim ChckBox As Object


Private Sub CommandButton1_Click()

    Dim cCheckBox As EventCheckBox
    Dim varControl As Object
    
    Set cvCheckBoxs = New Collection
    
    For Each varControl In Me.Controls
        If TypeName(varControl) = "CheckBox" Then
            Set cCheckBox = New EventCheckBox
            Set cCheckBox.cvCheckBox = varControl
            cvCheckBoxs.Add cCheckBox
        End If
    Next varControl

End Sub

Hope this helps!
 
Upvote 0
Hello Domenic,

Thank you very much for your help!
This resolved the issue, I knew was something simple that I was not seeing.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
Members
452,616
Latest member
intern444

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