I have a number of controls that are added during runtime but I want to populate a combobox that is created during runtime based on answers received from the the other dynamic controls (its hard to word)!
I will try and explain via my code! During runtime the following controls are added to my userform:
Set cLabel = Me.Controls.Add("Forms.Label.1")
With cLabel
.Caption = "Do you study maths?"
.Font.Size = 8
.Font.Bold = False
.Font.Name = "Tahoma"
.Left = 38
.Top = mathslbl
.Width = 220
End With
Set cManagedOptionButtonYes= Me.Controls.Add("Forms.OptionButton.1")
With cManagedOptionButtonYes
.Caption = "Yes"
.Name = "fibreRingYes" & i
.GroupName = "fibreRing" & i
.Left = 160
.Top = mathsYes
.Width = 100
.Height = 15.75
End With
ReDim Preserve TextListBox(1 To i)
Set TextListBox(i).mathsGroupYes = cManagedOptionButtonYes
Set cStudentDropdown = Me.Controls.Add("Forms.Combobox.1")
With cStudentDropdown
.Name = "StudenDropdown1" & (i)
.Left = 50
.Top = studentCombobox
.Width = 130
.Height = 18
End With
ReDim Preserve TextListBox(1 To i)
Set TextListBox(i).studentdropdown1Group = cStudentDropdown
So during runtime the following controls are created, these controls ask whether the student studies maths or not and dependant on the answer the user gives the combobox will be populated. Below is the code that is trying to populate the combobox based on the users answer to the question:
Private Sub cManagedOptionButtonYes_Click()
Dim rng As Range
Dim cell As Range
Dim c As Range
With cStudentDropdown
Sheets("Students").Activate
For Each c In Sheets("Students").Range("C14:C86")
.AddItem c.Value
Next
End With
End Sub
When the code tries to execute i get the following error Object variable or with block variable not set
and I can't figure out why this won't work for me!