TextBox to count values from ComboBox item

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
Hi good afternoon, all I'm trying to count when CommandButton3 is clicked in UserForm2 ComboBox2 value="Holiday" or "Stores" to count eachtime in UserForm1 TextBox10, but the code I have below doesn't seem to be counting anything. Please can you help.

VBA Code:
Private Sub CommandButton3_Click()
    If TextBox3.Value = "" Then
        MsgBox "Please enter 'Notes'"
        Exit Sub
    End If
    
    Dim emptyRow As Long
    Dim holidayCount As Integer
    Dim storesCount As Integer
    Dim i As Integer

    With ThisWorkbook.Sheets("Raised")
        emptyRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1

        .Cells(emptyRow, 1).Value = IIf(ComboBox1.Value = "", "NA", ComboBox1.Value)
        .Cells(emptyRow, 2).Value = IIf(TextBox1.Value = "", "NA", TextBox1.Value)
        .Cells(emptyRow, 3).Value = IIf(TextBox2.Value = "", "NA", TextBox2.Value)
        .Cells(emptyRow, 4).Value = IIf(ComboBox2.Value = "", "NA", ComboBox2.Value)
        .Cells(emptyRow, 5).Value = IIf(TextBox3.Value = "", "NA", TextBox3.Value)
    End With

    ' Count occurrences of "Holiday" and "Stores" in ComboBox2 of UserForm2
    For i = 0 To UserForm2.ComboBox2.ListCount - 1
        If UserForm2.ComboBox2.List(i) = "Holiday" Then
            holidayCount = holidayCount + 1
        ElseIf UserForm2.ComboBox2.List(i) = "Stores" Then
            storesCount = storesCount + 1
        End If
    Next i

    ' Update TextBox10 of UserForm1 with the counts of "Holiday" and "Stores"
    UserForm1.TextBox10.Value = holidayCount + storesCount

    ' Unload UserForm2
    Unload Me

    ' Show UserForm1
    If Not UserForm1.Visible Then
        UserForm1.Show
    End If
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi good afternoon, all I'm trying to count when CommandButton3 is clicked in UserForm2 ComboBox2 value="Holiday" or "Stores" to count eachtime in UserForm1 TextBox10, but the code I have below doesn't seem to be counting anything. Please can you help.

VBA Code:
Private Sub CommandButton3_Click()
    If TextBox3.Value = "" Then
        MsgBox "Please enter 'Notes'"
        Exit Sub
    End If
   
    Dim emptyRow As Long
    Dim holidayCount As Integer
    Dim storesCount As Integer
    Dim i As Integer

    With ThisWorkbook.Sheets("Raised")
        emptyRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1

        .Cells(emptyRow, 1).Value = IIf(ComboBox1.Value = "", "NA", ComboBox1.Value)
        .Cells(emptyRow, 2).Value = IIf(TextBox1.Value = "", "NA", TextBox1.Value)
        .Cells(emptyRow, 3).Value = IIf(TextBox2.Value = "", "NA", TextBox2.Value)
        .Cells(emptyRow, 4).Value = IIf(ComboBox2.Value = "", "NA", ComboBox2.Value)
        .Cells(emptyRow, 5).Value = IIf(TextBox3.Value = "", "NA", TextBox3.Value)
    End With

    ' Count occurrences of "Holiday" and "Stores" in ComboBox2 of UserForm2
    For i = 0 To UserForm2.ComboBox2.ListCount - 1
        If UserForm2.ComboBox2.List(i) = "Holiday" Then
            holidayCount = holidayCount + 1
        ElseIf UserForm2.ComboBox2.List(i) = "Stores" Then
            storesCount = storesCount + 1
        End If
    Next i

    ' Update TextBox10 of UserForm1 with the counts of "Holiday" and "Stores"
    UserForm1.TextBox10.Value = holidayCount + storesCount

    ' Unload UserForm2
    Unload Me

    ' Show UserForm1
    If Not UserForm1.Visible Then
        UserForm1.Show
    End If
End Sub
Hi good morning I didn’t mention that I want it to work as a combined coins not an individual count. Hope you understand and are able to help me with this please.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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