ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looping through ComboBoxes in a worksheet (https://www.excelbanter.com/excel-programming/298637-looping-through-comboboxes-worksheet.html)

TonyM

Looping through ComboBoxes in a worksheet
 
I have a number of worksheets with combobox lists in them.
What I want to do is populate the comboboxes with items
listed in named ranges on a master worksheet but am not
sure how I would loop through each combobox on each
worksheet.

The code for populating I already have. I need to look at
each combobox on each sheet and if combobox is cbOne then
populate with items from list1, cb2 - list2 etc.

Something along the lines of:-

Dim sht as worksheet, cb as comboBox

For each sht in ActiveWorkbook
For each cb in sht.Controls 'controls collection
_ 'will not work here
'add list items
Next
Next


Can anyone help ??

regards,

TonyM

Tom Ogilvy

Looping through ComboBoxes in a worksheet
 
dim cb as MsForms.Combobox
dim ole as OleObject
dim sh as Worksheet
for each sh in ActiveWorkbook.Worksheets
for each ole in sh.OleObjects
if typeof ole.Object is MSForms.Combobox then
set cb = ole.Object
if cb.Name = "cb1" then

elseif cb.Name = "cb2" then

end if
end if
Next
Next

--
Regards,
Tom Ogilvy

"TonyM" wrote in message
...
I have a number of worksheets with combobox lists in them.
What I want to do is populate the comboboxes with items
listed in named ranges on a master worksheet but am not
sure how I would loop through each combobox on each
worksheet.

The code for populating I already have. I need to look at
each combobox on each sheet and if combobox is cbOne then
populate with items from list1, cb2 - list2 etc.

Something along the lines of:-

Dim sht as worksheet, cb as comboBox

For each sht in ActiveWorkbook
For each cb in sht.Controls 'controls collection
_ 'will not work here
'add list items
Next
Next


Can anyone help ??

regards,

TonyM




No Name

Looping through ComboBoxes in a worksheet
 
excellent!

thanks, Tom

-----Original Message-----
dim cb as MsForms.Combobox
dim ole as OleObject
dim sh as Worksheet
for each sh in ActiveWorkbook.Worksheets
for each ole in sh.OleObjects
if typeof ole.Object is MSForms.Combobox then
set cb = ole.Object
if cb.Name = "cb1" then

elseif cb.Name = "cb2" then

end if
end if
Next
Next

--
Regards,
Tom Ogilvy

"TonyM" wrote in

message
...
I have a number of worksheets with combobox lists in

them.
What I want to do is populate the comboboxes with items
listed in named ranges on a master worksheet but am not
sure how I would loop through each combobox on each
worksheet.

The code for populating I already have. I need to look

at
each combobox on each sheet and if combobox is cbOne

then
populate with items from list1, cb2 - list2 etc.

Something along the lines of:-

Dim sht as worksheet, cb as comboBox

For each sht in ActiveWorkbook
For each cb in sht.Controls 'controls collection
_ 'will not work here
'add list items
Next
Next


Can anyone help ??

regards,

TonyM



.



All times are GMT +1. The time now is 06:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com