Populating a ComboBox (ActiveX) using a variable

WaqasTariq

Board Regular
Joined
Jun 26, 2012
Messages
58
Office Version
  1. 365
I have 100 combo boxes on an excel sheet, they are divided in three sets (1-25, 26-50, 51-100). Each set has the same value within the set, but is different from the other.

Right now I am able to populate the combo boxes using the following code
Code:
ActiveSheet.ComboBox1.AddItem "Value1"
ActiveSheet.ComboBox1.AddItem "Value2"
ActiveSheet.ComboBox1.AddItem "Value3"

^ This is time consuming and error prone. Is there a way I can use a variable to populate the combo boxes?

I have been able to find the following code, but it does not work.
Code:
Dim ComboBox As ObjectFor ComboBoxCalc1 = 1 To 25
    Set ComboBox = ComboBox(ComboBoxCalc1).Select
    ActiveSheet.ComboBox(ComboBoxCalc1).AddItem "Value1"
    ActiveSheet.ComboBox(ComboBoxCalc1).AddItem "Value2"
    ActiveSheet.ComboBox(ComboBoxCalc1).AddItem "Value3"
Next

I will appreciate any help!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try List and an array.
Code:
ActiveSheet.ComboBox1.List = Array("Item1", "Item2", "Item3")
 
Upvote 0
Perhaps.
Code:
For I = 1 To 25
    ActiveSheet.OLEObjects("ComboBox" & I).Object.List = Array("Value1", "Value2", "Value3")
Next I
 
Upvote 0
Try List and an array.
Code:
ActiveSheet.ComboBox1.List = Array("Item1", "Item2", "Item3")
That is going to be very convenient, thank you!

Though I will still like to be be able to use a variable so I can shorten what I have to write.
 
Upvote 0
Perhaps.
Code:
For I = 1 To 25
    ActiveSheet.OLEObjects("ComboBox" & I).Object.List = Array("Value1", "Value2", "Value3")
Next I
That would have been awesome to use, but unfortunately results in an error. Googling for the error, any ideas?

"Run-time error '1004':Unable to get the PLEObjects property of the worksheet class"
 
Upvote 0
What are the names of the comboboxes?

Is the worksheet with the comboboxes active?
 
Upvote 0
What are the names of the comboboxes?

Is the worksheet with the comboboxes active?
The name of the combox start with ComboBox1, ComboBox2, ComboBox3, and so on.
Yes, the worksheet with the combo boxes is active.

Never mind, got it to work. In my excitement I was trying to use the wrong variable for the for loop.

EDIT: Unable to find a way to mark this as answered. You guys are awesome, Thank you!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top