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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
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".
 
Upvote 0
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?
 
Upvote 0
Hi Paol:o,

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
 
Upvote 0
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.
 
Upvote 0
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?
 
Upvote 0
I was trying that earlier...

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

Gives type mismatch.
 
Upvote 0
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"
 
Upvote 0

Forum statistics

Threads
1,224,523
Messages
6,179,301
Members
452,904
Latest member
CodeMasterX

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