Results 1 to 7 of 7

Thread: Multi ComboBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    1

    Multi ComboBox

    Ok. So I have a userform I'm putting together that contains 20 combo boxes that will all refer to the same named range in my workbook. I was able to make it work but its pretty ugly. Any tips for a better way?

    For Each MainCode In [MainCodeList]
    Me.cmbMain1.AddItem MainCode
    Me.cmbMain2.AddItem MainCode
    Me.cmbMain3.AddItem MainCode
    Me.cmbMain4.AddItem MainCode
    Me.cmbMain5.AddItem MainCode
    Me.cmbMain6.AddItem MainCode
    Me.cmbMain7.AddItem MainCode
    Me.cmbMain8.AddItem MainCode
    Me.cmbMain9.AddItem MainCode
    Me.cmbMain10.AddItem MainCode
    Me.cmbMain11.AddItem MainCode
    Me.cmbMain12.AddItem MainCode
    Me.cmbMain13.AddItem MainCode
    Me.cmbMain14.AddItem MainCode
    Me.cmbMain15.AddItem MainCode
    Me.cmbMain16.AddItem MainCode
    Me.cmbMain17.AddItem MainCode
    Me.cmbMain18.AddItem MainCode
    Me.cmbMain19.AddItem MainCode
    Me.cmbMain20.AddItem MainCode
    Next MainCode
    Last edited by Shaggy Hiker; Apr 20th, 2018 at 05:34 PM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Multi ComboBox

    That looks like VBA, right?
    It should be in the Office Development Forum

  3. #3
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Multi ComboBox

    You can do something like this (substitute for the correct range in your case):

    Code:
    Private Sub UserForm_Click()
        Dim cmb As ComboBox
        
        For Each cmb In Me.Controls
            cmb.List = Worksheets("Sheet1").Range("a1:a5").Value
        Next
    End Sub

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Multi ComboBox

    @ bryce did you test?
    probably better to use the rowsource
    also if there are any other controls (textbox or whatever) a type mismatch will occur
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Multi ComboBox

    Yes, I tested, but to your point, without any other controls. Thanks for correcting!

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Multi ComboBox

    When I added other controls to the form, this worked:

    Code:
    Private Sub UserForm_Click()
        Dim ctrl As Control
        
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is MSForms.ComboBox Then
                ctrl.List = Worksheets("Sheet1").Range("a1:a5").Value
            End If
        Next
    End Sub
    Or, using row source, sub in this line:

    Code:
    ctrl.RowSource = "Sheet1!a1:a5"

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: Multi ComboBox

    McWaterMan,

    This is aircode (i.e., no testing), but you could also do something like the following. Also, this thread inspired me to mock up a framework for VBA control arrays, found here. However, that might be more than you'd like to tackle right now. But the following does make a loop out of what you're doing.

    Code:
    
    For Each MainCode In [MainCodeList]
        Dim ctl As Control
        For Each ctl In Me.Controls
            If TypeName(ctl) = "ComboBox" Then
                If Left$(ctl.Name, 7) = "cmbMain" Then
                    ctl.AddItem MainCode
                End If
            End If
        Next
    Next
    
    Enjoy,
    Elroy
    Last edited by Elroy; Apr 21st, 2018 at 06:00 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width