Count The Number of UserForm ComboBoxes Which Contain Specific Text

Nadine67

Board Regular
Joined
May 27, 2015
Messages
225
Hello and thank you for any attention my post may receive.

So I have an excel userform (UserForm1) with various controls and I would like to count the number of comboboxes (cbx) which contain the word 'Yes' and report the count in Label82 on the form. There are 24 cbx on UserForm1 and are numbered 1 - 24.

To make this even more tricky I would like the CountIf to execute once TextBox57 is no longer empty.

Is this possible?

Thank you and have a great day!
 
@Nadine67,

Make sure you read Messages #9 and #10



@GTO,

My approach to the Class solution is a bit different from yours (a touch more compact as I am wont to favor)...

Place in a Class Module (named Class1)
Code:
Public WithEvents ComboBoxGroup As ComboBox

Private Sub ComboBoxGroup_Change()
  UserForm1.ShowYesCount
End Sub

Place in a UserForm
Code:
Dim objComboBoxes() As New Class1

Private Sub TextBox57_Change()
  ShowYesCount
End Sub

Private Sub UserForm_Initialize()
  Dim cbCount As Long, Ctrl As Control
  For Each Ctrl In Me.Controls
    If TypeName(Ctrl) = "ComboBox" Then
      Ctrl.List = [{"Yes","No","Maybe"}]
      If Mid(Ctrl.Name, 9) <= 24 Then
        cbCount = cbCount + 1
        ReDim Preserve objComboBoxes(1 To cbCount)
        Set objComboBoxes(cbCount).ComboBoxGroup = Ctrl
      End If
    End If
  Next
  Set Ctrl = Nothing
End Sub

Sub ShowYesCount()
  Dim X As Long, Count As Long
  If Len(UserForm1.TextBox57.Text) Then
    For X = LBound(objComboBoxes) To UBound(objComboBoxes)
      If LCase(objComboBoxes(X).ComboBoxGroup.Value) = "yes" Then
        Count = Count + 1
      End If
    Next
    UserForm1.Label82.Caption = Count
  Else
    UserForm1.Label82.Caption = ""
  End If
End Sub
 
Last edited:
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Then added the 'ShowYesCount' to the two controls Change event.

Sorry for the confusion on my part. By this I meant the Txtbox and Label. I did add the code to all 24 cbx. The reason why your code didn't do as it should was because I didn't do as you said. I apologise for that. I actually added the TextBox57 code to the incorrect txtbox.

I picked it up when I came back in to work this morning and corrected my error. And presto.....the count is working as it should.

Thank you so much for your patience and help Rick. Have a great day!
 
Upvote 0

Forum statistics

Threads
1,216,526
Messages
6,131,187
Members
449,631
Latest member
mehboobahmad

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