Saad Mohammed
Saad Mohammed

Reputation: 1

vba how to add same values for multiple combo box?

I'm trying to add a list of numbers to multiple combo boxes at the same time with an easy way I tried to use (And) but it's not working

With Me.ComboBox10 and Me.ComboBox11
   
   .AddItem "0%"
   .AddItem "10%"
   .AddItem "20%"
   .AddItem "30%" ' and so on.....
   
End With

Upvotes: 0

Views: 754

Answers (2)

YasserKhalil
YasserKhalil

Reputation: 9568

Try this code

Private Sub UserForm_Initialize()
    Dim v, ctrl As Control
    v = Array("0%", "10%", "20%", "30%")
    For Each ctrl In UserForm1.Controls
        If TypeName(ctrl) = "ComboBox" Then
            If ctrl.Name = "ComboBox1" Or ctrl.Name = "ComboBox3" Then ctrl.List = v
        End If
    Next ctrl
End Sub

Upvotes: 1

Storax
Storax

Reputation: 12187

I would use a different approach as With Me.ComboBox10 and Me.ComboBox11 is not valid.

I asssume the ComboBoxes are on a Userform otherwise you have to adjust the code. But the approach does not change.

Private Sub UserForm_Initialize()
    
    Dim vDat As Variant
    ReDim vDat(0 To 5)
    vDat(0) = "0%"
    vDat(1) = "10%"
    vDat(2) = "20%"
    vDat(3) = "30%"
    vDat(4) = "40%"
    vDat(5) = "50%"
    
    Dim sngControl As MSForms.Control
    For Each sngControl In Me.Controls
        If TypeName(sngControl) = "ComboBox" Then
            sngControl.List = vDat
        End If
    Next
    
End Sub

Upvotes: 2

Related Questions