excel vba - ComboBox event handling in class collection problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wizofoz777
    New Member
    • Apr 2010
    • 1

    excel vba - ComboBox event handling in class collection problem

    Ok here is my problem : I was creating comboboxes and I wanted to be able to use the same event for all of them. So I decided to create a combobox collection containing the event in a wrapper. When testing this part was working fine til I decided to add the creation of these combobox. So when I am dynamically creating these combobox, it is no longer triggering my wrapper event but if I only use a loop to affect every combobox objects to my class collection it is working right. Here is the code used :

    Code in ThisWorkbook:
    '************** *************** *************** *************** *************** *************** ***********
    Private objComboBoxColl ection As CboCollection

    Private Sub Workbook_Open()
    Dim cOle As OLEObject

    'By putting this in comments, event is triggering
    'Create_ComboBo xes (ActiveSheet.ra nge("A1:A9"))

    Set objComboBoxColl ection = New CboCollection

    For Each cOle In ActiveSheet.OLE Objects
    objComboBoxColl ection.Add cOle.Object
    Next

    End Sub

    Function Create_ComboBox es(range As range)
    Dim cOle As OLEObject
    Dim myCell As range

    For Each myCell In range.Cells
    With myCell
    Set cOle = ActiveSheet.OLE Objects.Add _
    (ClassType:="Fo rms.ComboBox.1" , Link:=False, _
    DisplayAsIcon:= False, _
    Top:=.Top, _
    Left:=.Left, _
    Width:=.Width, _
    Height:=.Height )
    With cOle
    .Placement = xlMoveAndSize
    .Name = "cboboxcol" & myCell.Column & "row" & myCell.Row
    End With
    Set cOle = Nothing
    End With
    Next myCell
    End Function
    '************** *************** *************** *************** *************** *************** ***********

    'Code in Class CboCollection:
    '************** *************** *************** *************** *************** *************** ***********
    Private colCboboxes As New VBA.Collection

    Private Sub Class_Terminate ()
    Set colCbobox = Nothing
    End Sub

    Public Property Get ComboBoxes() As VBA.Collection
    Set ComboBoxes = colCboboxes
    End Property

    Public Function Add(objItem As Variant) As CboWrapper

    Set Add = New CboWrapper
    Set Add.ComboBox = objItem
    colCboboxes.Add Add

    End Function
    '************** *************** *************** *************** *************** *************** ***********

    'Code in CboWrapper:
    '************** *************** *************** *************** *************** *************** ***********
    Private WithEvents objCboBox As MSForms.ComboBo x

    Public Property Get ComboBox() As MSForms.ComboBo x
    Set CboBox = objCboBox
    End Property

    Public Property Set ComboBox(ByRef objNewValue As MSForms.ComboBo x)
    Set objCboBox = objNewValue
    objCboBox.AddIt em "A"
    objCboBox.AddIt em "B"
    objCboBox.AddIt em "C"
    End Property

    Private Sub Class_Terminate ()
    Set objCboBox = Nothing
    End Sub

    Private Sub objCboBox_Chang e()
    MsgBox objCboBox.Value
    End Sub
    '************** *************** *************** *************** *************** *************** ***********
Working...