How to loop through Userform ComboBoxes and perform actions

jmwbowen

Board Regular
Joined
Jul 27, 2012
Messages
58
I am struggling with VBA code to loop through ComboBoxes on a userform and perform actions. The userform has other controls on it besides ComboBoxes.

I've tried:

Code:
Private Sub UserForm_Initialize()
Dim ctrl As Control
numTemplates = Application.WorksheetFunction.Count(shtDSORTTemplates.Range("A:A"))
For i = 2 To numTemplates + 1
    Me.lstTemplates.AddItem Application.WorksheetFunction.Index(shtDSORTTemplates.Range("C:C"), i)
Next i


For Each ctrl In Me.Controls
    If TypeName(ctrl) = "ComboBox" Then
        For Each cell In shtTrainingAids.Range("tblTrainingAidsList[[#Data],[s_TrainingAidName]]")
            ctrl.AddItem cell.Value
        Next cell
        
    End If
Next ctrl
End Sub

The part in question is the 2nd "For" loop. Using that method of looping through controls doesn't give me access to properties that are specific to comboboxes, such as ".locked".

How can I loop through ONLY the comboboxes in the userform?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
declare another variable as MsFORMS.COMBOBOX

Private Sub UserForm_Initialize()
Dim ctrl As Control, cbox As MSForms.ComboBox
numTemplates = Application.WorksheetFunction.Count(shtDSORTTemplates.Range("A:A"))
For i = 2 To numTemplates + 1
Me.lstTemplates.AddItem Application.WorksheetFunction.Index(shtDSORTTemplates.Range("C:C"), i)
Next i


For Each ctrl In Me.Controls
If TypeName(ctrl) = "ComboBox" Then
Set cbox = ctrl

For Each cell In shtTrainingAids.Range("tblTrainingAidsList[[#Data],[s_TrainingAidName]]")
cbox.AddItem cell.Value
Next cell

End If
Next ctrl
End Sub
 
Upvote 0

Forum statistics

Threads
1,217,386
Messages
6,136,289
Members
450,002
Latest member
bybynhoc

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top