I have a problem with my VBA script in Excel. What I do is basically creating buttons that when pressed will create a set of two extra comboboxes in one of the sheet. This button can be pressed continuously to add more comboboxes.
These newly created comboboxes will behave like this:
- Created 2 Combobox
- Combobox1 will load some list in Control sheet
- Whenever an item in Combobox1 is selected, Combobox2 will load list of items to be added to Combobox2
The code for adding the button is like this
Sub Add_Criteria()
Dim controlNum As Integer
Dim name1 As String
Dim name2 As String
Dim oOle1 As OLEObject
Dim oOle2 As OLEObject
Dim uniqueString As String
Dim cb1 As Object
controlNum = Sheets("Controls").Range("A16").Value
'adding Control
Set oOle1 = Sheets("System").OLEObjects _
.Add(ClassType:="Forms.ComboBox.1", Left:=10, _
Top:=75 + (controlNum * 20), Width:=100, Height:=18)
Set oOle2 = Sheets("System").OLEObjects _
.Add(ClassType:="Forms.ComboBox.1", Left:=120, _
Top:=75 + (controlNum * 20), Width:=100, Height:=18)
'adding properties
oOle1.Name = "Criteria" & controlNum * 2 - 1
oOle2.Name = "Criteria" & controlNum * 2
'adding control var
Sheets("Controls").Range("A16").Value = controlNum + 1
With oOle1.Object
.List = Sheets("Controls").Range("A5:A13").Value
End With
End Sub
The question is, I cannot detect events on it. I need to change the value shown on the second combobox created when value in combobox1 changed. I tried to use below reference and I still can't. Can anyone guide me on how to do this
Reference (Been on this Problem for days):
http://www.dbforums.com/microsoft-excel/1641165-detecting-click-event-dynamically-created-controls.html (This is for userform, I don't know why I can't replicate this in Sheet)