So I'm dynamically adding a series of ActiveX checkboxes to a worksheet. Each checkbox represents a given product and the set of products varies based on the market I select in a combo box drop down. I have a class module that should control what happens on the click event for each newly added checkbox. I'm having issues assigning the checkboxes to the class.
Here is the class module, which works fine once I get all the checkboxes assigned to the class.
Here is the code on the worksheet
This routine then gets called as part of the change event for the market drop down list. Everything works fine, except nothing gets added to the drugcb array. If I split out the code as a separate routine (as below) and run it after the checkboxes are created, everything works fine.
I've tried both calling the "AddToClass" routine in the change event of the market drop down and adding the checkboxes to the array as they are created (as shown above). In either case, my array comes up empty. Since the "AddToClass" routine works just fine when I run it manually after everything is created, my guess is that I'm screwing up either how the checkboxes are assigned to the array or misunderstand the timing of how the checkboxes are added and when I may start referencing them.
Any help would be greatly appreciated.
Thanks,
Swetz
Here is the class module, which works fine once I get all the checkboxes assigned to the class.
Code:
Public WithEvents CBDrugGroup As MSForms.CheckBox
Private Sub CBDrugGroup_Click()
With Worksheets("ProductGraph")
Call UpdateDrugGraph(.cbMarket.Value, CBDrugGroup.Caption, .ChartObjects("DrugGraph").Chart, CBDrugGroup.Value, True)
End With
End Sub
Here is the code on the worksheet
Code:
Option Explicit
'Create a collection of controls that follow my new class for updating the state graph.
Dim drugcb() As New cbDrugGraph
Sub CreateCheckboxes(ByVal Market As String)
'Creates a series of checkboxes per market on the worksheet
Dim ToRow As Long, LastRow As Long, MyLeft As Double, MyTop As Double, MyHeight As Double, MyWidth As Double
Dim cb As OLEObject, marketRange As Range, FirstRow As Long, RowCount As Long, ProdListData As Variant, col As Integer
Dim counter As Long
'Determine how many products are in a given market
With Worksheets("ProdList")
FirstRow = Application.Match(Market, .Range("A:A"), 0)
RowCount = Application.WorksheetFunction.CountIf(.Range("A:A"), Market)
Set marketRange = .Range("A1").Offset(FirstRow - 1, 0).Resize(RowCount, 2)
ProdListData = marketRange.Value
Set marketRange = Nothing
End With
'We will only display checkboxes in the first 2 columns, so if there are a lot of products, it will go far down the page
LastRow = UBound(ProdListData, 1) / 2
counter = 0
'Clear the old array of controls
ReDim drugcb(1 To 1)
For col = 1 To 2
For ToRow = 5 To LastRow + 5
counter = counter + 1
If counter > UBound(ProdListData, 1) Then Exit For
'Get the position from the cells
MyLeft = Cells(ToRow, col).Left
MyTop = Cells(ToRow, col).Top
MyHeight = Cells(ToRow, col).Height
MyWidth = MyHeight = Cells(ToRow, col).Width + 50
Set cb = ActiveSheet.OLEObjects.Add(classtype:="Forms.Checkbox.1", _
Top:=MyTop, Left:=MyLeft, _
Height:=MyHeight, Width:=MyWidth)
With cb.Object
.Caption = ProdListData(counter, 2)
.Value = False
End With
'Add the new checkbox to the array assigned to my class
ReDim Preserve drugcb(1 To counter)
Set drugcb(counter).CBDrugGroup = cb.Object
Set cb = Nothing
Next ToRow
Next col
End Sub
This routine then gets called as part of the change event for the market drop down list. Everything works fine, except nothing gets added to the drugcb array. If I split out the code as a separate routine (as below) and run it after the checkboxes are created, everything works fine.
Code:
Sub AddtoClass()
'Adds the existing checkboxes to my custom class
Dim boxcount As Integer
Dim oleObj As OLEObject
boxcount = 0
'Clear the old one
ReDim drugcb(1 To 1)
For Each oleObj In OLEObjects
If oleObj.progID = "Forms.CheckBox.1" Then
boxcount = boxcount + 1
ReDim Preserve drugcb(1 To boxcount)
Set drugcb(boxcount).CBDrugGroup = oleObj.Object
End If
Next oleObj
End Sub
I've tried both calling the "AddToClass" routine in the change event of the market drop down and adding the checkboxes to the array as they are created (as shown above). In either case, my array comes up empty. Since the "AddToClass" routine works just fine when I run it manually after everything is created, my guess is that I'm screwing up either how the checkboxes are assigned to the array or misunderstand the timing of how the checkboxes are added and when I may start referencing them.
Any help would be greatly appreciated.
Thanks,
Swetz