Counting Textbox vakue using CountIf - Excel VBA

CakzPrimz

Board Regular
Joined
Oct 6, 2017
Messages
57
Hi,

I have 3 textboxes in number format. In texbox1 value = 11, textbox2 value = 5 and textbox 3 value = 11.
And I want to count how many number of 11 in a textbox4, every time the value in textbox1 is changed or update.

Here's the code I wrote and not working:
Code:
Private Sub TextBox1_AfterUpdate()
    On Error Resume Next
    TextBox4.Value = Application.CountIf(TextBox1.Value, 11) + Application.CountIf(TextBox2.Value, 11) +Application.CountIf(TextBox3.Value, 11)
End Sub

Thank you
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If your textboxes are so named and you really want to always use the afterupdate of TextBox1, then

Try this
Code:
Private Sub TextBox1_AfterUpdate()
    Dim I As Long, Cnt As Long
    For I = 1 To 3
        If Me.Controls("TextBox" & I).Value = 11 Then Cnt = Cnt + 1
    Next I
    TextBox4.Value = Cnt
End Sub
 
Last edited:
Upvote 0
Hi,
CountIf I think you will find works with Ranges.

Untested but see if following update to your code does what you want

Code:
Private Sub TextBox1_AfterUpdate()


   Me.TextBox4.Value = Application.Count(Application.Match(Array(Val(Me.TextBox1.Value), _
                                                                Val(Me.TextBox2.Value), _
                                                                Val(Me.TextBox3.Value)), Array(11), 0))
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,215,811
Messages
6,127,017
Members
449,351
Latest member
Sylvine

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