Loop through combo boxes

Paella1

Active Member
Joined
Mar 10, 2005
Messages
382
I have a number of combo boxes on a worksheet (not a userform).

I want to loop through them and populate the list froma named range.

How do I loop through the combo boxes? Ie for each <i>something</i> in <i>somethings</i>.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

wut

Banned
Joined
Dec 13, 2010
Messages
229
For each cb in ActiveSheet.ComboBoxes
code (eg, cb.listfillrange:=range("x"))
Next

I don't know if the collection for "Comboboxes" is "comboboxes". Alternatively, if you have only comboboxes on your worksheet, you could use the collection "OLEObjects".
 

Paella1

Active Member
Joined
Mar 10, 2005
Messages
382
How do I loop through OLEObjects? the command "for each cb in OLEObjects" generates a type mismatch.

Also what type of OLEObject is a combo box?
 

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
4,735
Hi Paol:eek:,

Based on this post try this:

Code:
For Each OLEObject In ActiveSheet.OLEObjects
        With OLEObject
            If InStr(.progID, "ComboBox") > 0 Then
                'Paol:o, put your relevant code here you that you wish to run when the _
                ActiveX control found on the activesheet is a ComboBox
            End If
            
        End With
    Next OLEObject

You can't use the type of an OLEObject (i.e. OLEObject.OLEType) as it returns 2 for all types of ActiveX controls.

HTH

Robert
 

Paella1

Active Member
Joined
Mar 10, 2005
Messages
382

ADVERTISEMENT

Thanks Robert. It works fine ... as far as the looping bit goes

Another problem though: if I try to populate the combo box using OLEobject.List I get "object doesn't support this property or method".

But if I use the full name of the combobox e.g. ComboBox1.List then it works fine.
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,520
Try..

Code:
[font=Verdana]    [color=darkblue]For[/color] [color=darkblue]Each[/color] OLEObject [color=darkblue]In[/color] ActiveSheet.OLEObjects
        [color=darkblue]With[/color] OLEObject
            [color=darkblue]If[/color] InStr(.progID, "ComboBox") > 0 [color=darkblue]Then[/color]
                .ListFillRange = Range("benefittype").Name
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]Next[/color] OLEObject
    [/font]

So each ComboBox will have the same values?
 

wut

Banned
Joined
Dec 13, 2010
Messages
229

ADVERTISEMENT

I was trying that earlier...

ActiveSheet.ComboBox1.ListFillRange = Range("A1:A10")

Gives type mismatch.
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,520
I was trying that earlier...

ActiveSheet.ComboBox1.ListFillRange = Range("A1:A10")

Gives type mismatch.

The ListFillRange property requires a string to set the worksheet range...

ActiveSheet.ComboBox1.ListFillRange = "A1:A10"
 

Watch MrExcel Video

Forum statistics

Threads
1,130,167
Messages
5,640,539
Members
417,151
Latest member
ChickenTenderer

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
Top