Results 1 to 9 of 9

Thread: [RESOLVED] [Excel - VBA] How to add the same items to multiple combo boxes?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    2

    Resolved [RESOLVED] [Excel - VBA] How to add the same items to multiple combo boxes?

    Hi everyone,

    I'm new to VBA-Excel and was wondering if you would happen to know how to add the same items to multiple combo boxes.

    I want to set up 31 combo boxes that will contain the same items, without having to do it manually.

    So far this is what my code looks like:
    Code:
        With Worksheets("January")
        
        .ComboBox1.Clear
        .ComboBox1.AddItem "Regular"
        .ComboBox1.AddItem "Vacation"
        .ComboBox1.AddItem "Statutory"
        .ComboBox1.AddItem "CT - Half"
        .ComboBox1.AddItem "CT - Full"
        .ComboBox1.Text = .ComboBox1.List(0)
        
        .ComboBox2.Clear
        .ComboBox2.AddItem "Regular"
        .ComboBox2.AddItem "Vacation"
        .ComboBox2.AddItem "Statutory"
        .ComboBox2.AddItem "CT - Half"
        .ComboBox2.AddItem "CT - Full"
        .ComboBox2.Text = .ComboBox2.List(0)
        
        .ComboBox3.Clear
        .ComboBox3.AddItem "Regular"
        .ComboBox3.AddItem "Vacation"
        .ComboBox3.AddItem "Statutory"
        .ComboBox3.AddItem "CT - Half"
        .ComboBox3.AddItem "CT - Full"
        .ComboBox3.Text = .ComboBox3.List(0)
    
    End With
    As you can see it's very repetitive and time consuming.
    Any help would be greatly appreciated.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [Excel - VBA] How to add the same items to multiple combo boxes?

    Welcome to the forums

    It is still going to be repetitive, but perhaps not quite so much.

    Put all that code in a sub routine and call it for each of your combo boxes.
    Code:
    Public Sub LoadCombos(pcb As Combobox)
    With pcb
        .Clear
        .AddItem "Regular"
        .AddItem "Vacation"
        .AddItem "Statutory"
        .AddItem "CT - Half"
        .AddItem "CT - Full"
        .Text = .List(0)
    End With
    End Sub
    
    Private Sub CommandButton1_Click()
    LoadCombos CombBox1
    LoadCombos CombBox2
    LoadCombos CombBox3
    etc
    End Sub
    If you are using a UserForm, perhaps something like this would be useful
    Code:
    Dim ctrl As Control
         For Each ctrl In Me.Controls
           If TypeName(ctrl) = "Combobox" Then
               LoadCombos ctrl.Name
           End If
        Next
    Last edited by Hack; Mar 16th, 2010 at 12:10 PM.

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

    Re: [Excel - VBA] How to add the same items to multiple combo boxes?

    if the comboboxes are on a worksheet you can loop through the shapes or oleobjects collection of the sheet

    vb Code:
    1. for i = 1 to 31
    2.   me.shapes("combobox" & i).oleformat.object.object.additem "test"
    3. next
    if the code is in a module you need to specify the sheet by name or index, instead of using the ME keyword
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    2

    Resolved Re: [Excel - VBA] How to add the same items to multiple combo boxes?

    Quote Originally Posted by westconn1 View Post
    if the comboboxes are on a worksheet you can loop through the shapes or oleobjects collection of the sheet

    vb Code:
    1. for i = 1 to 31
    2.   me.shapes("combobox" & i).oleformat.object.object.additem "test"
    3. next
    if the code is in a module you need to specify the sheet by name or index, instead of using the ME keyword
    This is EXACTLY what i needed!

    Great stuff and thank you both for all your help

  5. #5
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Re: [Excel - VBA] How to add the same items to multiple combo boxes?

    Quote Originally Posted by Hack View Post
    Code:
    Dim ctrl As Control
         For Each ctrl In Me.Controls
           If TypeName(ctrl) = "Combobox" Then
               LoadCombos ctrl.Name
           End If
        Next
    Newbie here too..tried this code but i get an error "Type Mismatch".

    How do I resolve this?

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] [Excel - VBA] How to add the same items to multiple combo boxes?

    Welcome to VBForums

    There is a minor mistake on this line:
    Code:
               LoadCombos ctrl.Name
    .. change it to:
    Code:
               LoadCombos ctrl

  7. #7
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Re: [RESOLVED] [Excel - VBA] How to add the same items to multiple combo boxes?

    Thank you so much for your reply...code now works.

    If you don't mind asking, i'm still pretty much confused how pass by ref and pass by val work. Why didn't it work? ctrl.name would still give "Name1"?

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] [Excel - VBA] How to add the same items to multiple combo boxes?

    The problem was that ctrl.Name just passes a String, but the data type of the parameter for the sub is Combobox rather than String. It doesn't matter that the String happens to contain the name of a combobox, it is still just a String.

    Removing the .Name means that the entire combobox gets passed, so the sub can then accept it, and work with it (in this case, adding items to the list).

  9. #9
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: [RESOLVED] [Excel - VBA] How to add the same items to multiple combo boxes?

    byval uses a copy of the value of the variable passed

    byref uses the actual variables contents

    byval can never change the variables external value - the one held outside the procedure

    byref can change the value outside the procedure and causes "side affects" which are not always wanted

    hope that clear that up

    as si-the-geek said ctrl and ctrl.name may look the same but they are nothing like the same

    think of the object ctrl as forrest gump's box of chocolates

    you can ask forrest for the box or the name on the box

    only one of these things will actually contain the chocolates

    here to analogize ( I think thats a real word )
    Last edited by incidentals; Jan 16th, 2012 at 09:35 AM.

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