Problems with VBA countif & criteria with range containing decimal numbers

khufu

New Member
Joined
Mar 23, 2015
Messages
1
To cut the long story short, I can't seem to get a very simple VBA code containing WorksheetFunction.CountIf and criteria range with decimal numbers to work properly. I encountered the problem while working on a project, and have tried the following test code in Excel 2007 and Excel 2011 for Mac. In both cases the message box reads "0". However, if I replace the criteria range with number (for example "0,5") I get the correct result (for example "3"). Same applies if I type a whole number in the criteria range (for example, type "2" instead of 0,5 in the cell A4).

I have a worksheet containing whole and decimal numbers in a column A, and I have the following VBA code:

Sub countiftest()
MsgBox WorksheetFunction.CountIf(Range("A1:A8"), "=" & Range("A4").value)
End Sub

It should be clear that the message box should print number 1 or higher. However, if the worksheet has a decimal number in the cell A4, I get 0 every time.

I use comma as a decimalseparator on my mac, and dot on my PC. If I use the similar CountIf function on the worksheet as a standard Excel formula, it returns the correct result.

Any thoughts?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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