ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   For Each Combobox (https://www.excelbanter.com/excel-discussion-misc-queries/242242-each-combobox.html)

jlclyde

For Each Combobox
 
Can you use a For each statement in VBA to go through the comboboxes
on a form and tell you how many are filled in? This woudl be easier
then listing them all in the code and doing if statements for each
one.

Thanks,
Jay

Dave Peterson

For Each Combobox
 
If you named them nicely and knew how many there were, you could use:

dim cCtr as long
for cctr = 1 to 5 '5 comboboxes
with me.controls("combobox" & cctr)
if .listindex < 0 then
msgbox "combobox" & cctr & " is empty"
end if
end with
next cctr

If you didn't know how many there were or they weren't named nicely:

Dim ctrl as control
for each ctrl in me.controls
if typeof ctrl is msforms.combobox then
if ctrl.listindex < 0 then
msgbox ctrl.name & " is empty"
end if
end if
next ctrl


jlclyde wrote:

Can you use a For each statement in VBA to go through the comboboxes
on a form and tell you how many are filled in? This woudl be easier
then listing them all in the code and doing if statements for each
one.

Thanks,
Jay


--

Dave Peterson

Jim Thomlinson

For Each Combobox
 
You need to traverse through all of the controls and determine which ar combo
boxes something like this...

Dim ctl As Control
Dim lng As Long

For Each ctl In Me.Controls
If TypeOf ctl Is msforms.ComboBox Then lng = lng + 1
Next ctl
MsgBox lng
--
HTH...

Jim Thomlinson


"jlclyde" wrote:

Can you use a For each statement in VBA to go through the comboboxes
on a form and tell you how many are filled in? This woudl be easier
then listing them all in the code and doing if statements for each
one.

Thanks,
Jay


jlclyde

For Each Combobox
 
This is what I ended up with. I needed it to count how many had
things in it. I may not have been as clear as I needed to be. Thanks
for your help and Dave's.
Thanks,
Jay

Dim ctl As Control
Dim lng As Long

For Each ctl In Me.Controls
If TypeOf ctl Is msforms.ComboBox Then
If ctl.Value < "" Then lng = lng + 1
End If

Next ctl
MsgBox lng



All times are GMT +1. The time now is 12:18 AM.

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