Using ComboBox Tag property to simply code

markgrnh

New Member
Joined
Apr 7, 2023
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Good morning.

I trust someone who is more VBA savvy with me can answer my query.

I have a Userform that contains multiple comboboxes, about 25 at the moment that I use to select different options to filter a table and export the results.

At the moment, I have written the code so it checks each combobox to see if the value is blank before running the actual filtering and export, and then gives a warning message, stopping the code until all boxes have a value. Now that is a lot of code, as each combobox has its own line of code, checking for blanks. Eg If CMBox1.value = "" then msgbox "You havent selected all options" exit sub end if blah blah etc... thats not actual code BTW :)

I have been thinking of ways to improve the code, so my question is, it is possible I can group all the comboboxes using the tag property, so that when it looks for blanks, it looks at them as a bulk. For example, If the item is a combobox and has a tag of "testing" then if any combobox value is "" then msgbox "You havent selected all options" exit sub etc.

Something along those lines, yet I havent done anything with grouping comboboxes as a group or how to actual do the code, so hoping someone on here can help?

Many thanks

Mark
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You don't need to group them, just loop through the controls and test the type:

Code:
   Dim ctl As MSForms.Control
   For Each ctl In Me.Controls
      If TypeName(ctl) = "ComboBox" And ctl.Tag = "testing" Then
         ' do your thing
         Exit Sub
      End If
   Next

for example.
 
Upvote 0
You don't need to group them, just loop through the controls and test the type:

Code:
   Dim ctl As MSForms.Control
   For Each ctl In Me.Controls
      If TypeName(ctl) = "ComboBox" And ctl.Tag = "testing" Then
         ' do your thing
         Exit Sub
      End If
   Next

for example.
That was quick thanks Rory. Also sorry, might be being dense, but I want it to bring up a msgbox if any of the values are blank, how to add that your code above, would it be crl.value = "" ? so in full;
If TypeName(ctl) = "ComboBox" And ctl.Tag = "testing" And ctl.value = "" Then
Msgbox "Not all values are selected, please check and try again"

Thanks
 
Upvote 0
Put that part into the ' do your thing section - e.g.:

Code:
   Dim ctl As MSForms.Control
   For Each ctl In Me.Controls
      If TypeName(ctl) = "ComboBox" And ctl.Tag = "testing" Then
         If ctl.Value = "" then
             msgbox "Not all options selected"
            ctl.setfocus
            Exit Sub
         end if
      End If
   Next
 
Upvote 1
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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