What vba code equivalent to excel formula CountIf

Flexcel22

Board Regular
Joined
Apr 8, 2016
Messages
52
Hello Mr/Mrs Excel(s),

This is might be a piece of cake if I were a MVP or expert in excel but I'm not. I went around circles on the internet and couldnt find what I desired.

Here's my case.

I had a textbox which I wanted to return the COUNTIF result after pressing a CommandButton. I know in excel formula you just insert, for example, =COUNTIF(F10:F20236, "<3.1") but since i wanted to be done on a userform, I began to stress out any ways.

Please help me out.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You can use the COUNTIF function in VBA like this:
Code:
Application.WorksheetFunction.Countif(...)
 
Upvote 0
Solution
Try, in VBA, merely putting "Worksheetfunction.CountIf(Range("F10:F20236"), "<3.1")

alternate syntax could be:

worksheetfunction.countif([b16:b55],"<24")
 
Last edited:
Upvote 0
How about
Code:
Private Sub CommandButton1_Click()

    TextBox1.Value = [COUNTIF(F10:F20236, "<3.1")]

End Sub
 
Upvote 0
Hello Fluff,

I have tried your code and it worked too

Thanks a lot for the tips and solutions. I really enjoy being part of this forum with many heroes.

You all save my day
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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