Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating multiple comboboxes in VBA

Status
Not open for further replies.

quick62silver

IS-IT--Management
Jan 14, 2009
4
US
I am new to using the combobox. All of the comboboxes are in the same column and each have the same four items to select. I am not using a Userform. I want to use VBA to populate the multiple comboboxes. I do not want to specify each combobox individually as follows

ComboBox1.AddItem "Item 1"
ComboBox1.AddItem "Item 2"
ComboBox1.AddItem "Item 3"
ComboBox1.AddItem "Item 4"
ComboBox2.AddItem "Item 1"
ComboBox2.AddItem "Item 2"
ComboBox2.AddItem "Item 3"
ComboBox2.AddItem "Item 4"
...
ComboBoxN.AddItem "Item 1"
ComboBoxN.AddItem "Item 2"
ComboBoxN.AddItem "Item 3"
ComboBoxN.AddItem "Item 4"

Is there a way to populate all of the comboboxes in a loop instead of specifying them like above?

Please advise.
 



Hi,

What is the purpose? This is not how multiple comboboxes are usually employed.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 



I'd GUESS that you want to populate multiple rows in a column from a combobox?

You could use Data > Validation -- LIST in that range, if that's the case.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks Skip for the quick reply. Sorry that this is not explained completely but here are more details. Hopefully this helps.

The column will have a combobox (ActiveX control) on each row for let's say 100 rows. They would contain the same 4 items for each combobox. The user is to select which of the 4 items they want to for that row and subsequently enter information on that row that further identifies the row. I wanted to populate each one of these comboboxes through VBA when the Worksheet becomes Active. I can populate these comboboxes individually fine by specifying each of the comboboxes AddItem property. So if I had 100 rows I would have 400 "AddItem" properties, 4 for each combobox. Obviously, this is cumbersome.

I am simply looking for a way, if possible, to maybe use a looping mechanism in VBA to "walk through" each of the 100 comboboxes to populate each one with the 4 items.

If this is not possible (or I am using this in the wrong manner) then what would be the best approach to do this? I am building this in Excel 2007 but it needs to work in Excel 2003 as well.

Thanks in advance

 


As I stated in my surmise, you can use the Data > Validation -- LIST feature, without any VBA!



Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Agreed that data validation could be the way to go but as I don;t get to code vba much at the mo I thought I would answer the specific question anyway:
Code:
Dim ctl As OLEObject

Dim cb As ComboBox

For Each ctl In ActiveSheet.OLEObjects

    If ctl.OLEType = 2 Then

        Set cb = ctl.Object

        cb.Clear

        cb.AddItem 1
        cb.AddItem 2
        cb.AddItem 3
        cb.AddItem 4
   
    End If

Next ctl

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 


If I were going to use a combobox control, I would insert ONE control on the sheet and position, size & make it visible, in the Worksheet_SelectionChange event.

Then asign the combobox result to the row.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
agreed - :)

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top