Adding items to ComboBox in a loop?

C

cmpcwil2

Using vb comboBox is it possible to add the same items to numerous
comboBox on the same sheet?
I have been trying the following....

Dim mycntrl As OLEObject
Dim sht As Worksheet
Set sht = ActiveSheet

For Each mycntrl In sht.OLEObjects

mycntrl.addItem "item1"

Next mycntrl

However I noticed that the addItem method is not available when trying
this, so how would you add an item in a loop?

thank you in advance for any help
 
D

Doug Glancy

cmpcwil2,

This syntax is difficult for me to remember, but I think this is what you
want:

Dim mycntrl As OLEObject
Dim sht As Worksheet

Set sht = ActiveSheet
For Each mycntrl In sht.OLEObjects
If TypeOf mycntrl.Object Is ComboBox Then
mycntrl.Object.AddItem "item1"
End If
Next mycntrl

hth,

Doug
 
G

Guest

Dim mycntrl As OLEObject
Dim sht As Worksheet
Set sht = ActiveSheet

For Each mycntrl In sht.OLEObjects
if typeof mycntrl.object is MSforms.Combobox then
mycntrl.Object.addItem "item1"
end if

Next mycntrl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top