I have a userform and a Click button on it. On click of this button a combobox is dynamically created. I want to do something when a particular value is selected from this combobox but the change event is not getting triggered. What could be the reason. Here is my code which is put in the UserForm1 module.
Private WithEvents ComboBox1 As MSForms.ComboBox
Private Sub ClickButton_Click()
'Create combo box
Dim ComboBox1 As MSForms.ComboBox
Set ComboBox1 = Me.Controls.Add("Forms.ComboBox.1")
With ComboBox1
.Left = 160
.Top = 50
.Width = 70
.Height = 20
.AddItem ("> than")
.AddItem ("< than")
.AddItem ("Max")
.AddItem ("Min")
.Enabled = True
.BackColor = RGB(255, 255, 255)
.ForeColor = RGB(0, 0, 0)
.SpecialEffect = fmSpecialEffectFlat
.Font.Size = 12
.Font.Bold = False
.Font.Name = "Arial"
.TabIndex = 2
End With
DoEvents
ComboBox1.SetFocus
End Sub
Private Sub ComboBox1_Change()
Dim inputNumber As Variant
If ComboBox1.Value = "> than" Then
inputNumber = InputBox("Enter a number:")
'Check if the input is valid number
If IsNumeric(inputNumber) Then
ComboBox1.Value = ComboBox2.Value & " " & inputNumber
Else
MsgBox "Invalid input"
End If
End If
End Sub