0

I want to make 3 different comboboxes in an Excel UserForm. In total there will be x of each type.

I want to make a code like this:

With ComboboxAi for i = 1 to 5
    .AddItem "monday"
    .AddItem "tuesday"

With ComboboxBj for j = 1 to 6
    .AddItem "january"
    .AddItem "february"

My comboboxes are named like: ComboboxA1, .., ComboboxA5, ComboboxB1, .., ComboboxB6

Can someone help me make the proper code?

1 Answer 1

2

This is the syntax:

    Dim i As Long

    For i = 1 To 5
        With Controls("ComboboxA" & i)
            .AddItem "Monday"
            .AddItem "Tuesday"
        End With
    Next i

    For i = 1 To 6
        With Controls("ComboboxB" & i)
            .AddItem "January"
            .AddItem "February"
        End With
    Next i

However, the following could be a better way to achieve the same thing:

    Dim ctrl As MSForms.Control

    For Each ctrl In Controls
        If TypeOf ctrl Is MSForms.ComboBox Then
            If ctrl.Name Like "ComboboxA*" Then ctrl.List = Array("Monday", "Tuesday")
            If ctrl.Name Like "ComboboxB*" Then ctrl.List = Array("January", "February")
        End If
    Next ctrl
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.