Home > Net >  vba how to add same values for multiple combo box?
vba how to add same values for multiple combo box?

Time:03-26

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

CodePudding user response:

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

CodePudding user response:

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
  •  Tags:  
  • vba
  • Related