VBA COUNTIF doesn't work with decimal numbers?

Instantaneo

New Member
Joined
Dec 5, 2012
Messages
28
Hi,

This question started in another topic but since it "jumps out" of it a bit and I figured out one reason for my code not working I'd like to came up with another topic to discuss this specific issue.

I have the following range (small set):

Values (avg)
0,18869
0,18788
0,16756
0,1146
0,12558
0,18118
0,17898
0,06433
0,1628
0,18928

<tbody>
</tbody>

I'm trying to use this code:

Code:
    Dim pivot_total As Integer    Dim range_pivot As String
    Dim lim_max As Double
    Dim ws As Worksheet
    Dim count_1 As Integer

    Set ws = Worksheets("KPI")    ws.Select
    pivot_total = Application.WorksheetFunction.Match("Grand Total", range("K:K"), 0) 
    range_pivot = "T6:T" & (pivot_total - 1) 'this is the range of those values above
    lim_max = 0.2
    count_1 = Application.WorksheetFunction.CountIf(ws.range(range_pivot), "<" & lim_max)
    MsgBox count_1

This code isn't working (it returns always 0). But now imagine this range:

Values (avg)
18
19
17
11
13
17
6
16
18

<tbody>
</tbody>
If I use this code...

Code:
    Dim pivot_total As Integer    Dim range_pivot As String
    Dim lim_max As Double
    Dim ws As Worksheet
    Dim count_1 As Integer

    Set ws = Worksheets("KPI")    ws.Select
    pivot_total = Application.WorksheetFunction.Match("Grand Total", range("K:K"), 0) 
    range_pivot = "T6:T" & (pivot_total - 1) 'this is the range of those values above
    lim_max = 15
    count_1 = Application.WorksheetFunction.CountIf(ws.range(range_pivot), "<" & lim_max)
    MsgBox count_1

... my count_1 will return 3.

So, my question is: why isn't COUNTIF working with small values? :(

Thank you for your help.
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Perhaps it's not the small numbers but the decimal separator that's causing the confusion. What happens if you use "<0,2" in the countif?

WBD
 
Last edited:
Upvote 0
Perhaps it's not the small numbers but the decimal separator that's causing the confusion. What happens if you use "<0,2" in the countif?

WBD

Hi!

Your suggestion, along with the one from CJ in the other topic, already have made me think about the decimal separator. But with your comment I tested other options:

a) I tested "< 0,2" as you said and it returned 0 as usual.
b) I tested "< 0.2" and it returned the correct value.

So it definitely must be related with the decimal separator...

So, how can I solve it? As you can see in the picture below, if I set it manually "lim_max = 0.2" and use lim_max in the expression, it doesn't work because "lim_max" takes the value of 0,2. But if I use "<0.2" directly (which I didn't want, since I'll have this for more than one row), it works...

bevehx.png


Thank you for your help!
 
Upvote 0
Then you could maybe just force the conversion:

Code:
Application.WorksheetFunction.CountIf(ws.range(range_pivot), Replace("<" & lim_max,",","."))

WBD
 
Upvote 0
Thank you so much WBD! I tried something like that but apparently in the wrong place. You suggested the right place! :biggrin:
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,860
Members
449,472
Latest member
ebc9

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