CountIf non-blank cells

YasserKhalil
PlatinumLounger
Posts: 4930
Joined: 31 Aug 2016, 09:02

CountIf non-blank cells

Post by YasserKhalil »

Hello everyone
I am trying to use this line

Code: Select all

Debug.Print Application.WorksheetFunction.CountIf(ws.Range("A4:A9"), "<>""""")

I got the result 6 while there are only two cells not empty. Any idea?

I also tried CountA `Debug.Print Application.WorksheetFunction.CountA(ws.Range("A4:A9"))` but the same result `6`

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: CountIf non-blank cells

Post by HansV »

All cells with a formula will be counted too, even if the formula returns an empty string.
Try

Debug.Print [SUMPRODUCT(--(A4:A9<>""))]
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4930
Joined: 31 Aug 2016, 09:02

Re: CountIf non-blank cells

Post by YasserKhalil »

there are no fomrulas and even your suggestion results in 6. That's weird

YasserKhalil
PlatinumLounger
Posts: 4930
Joined: 31 Aug 2016, 09:02

Re: CountIf non-blank cells

Post by YasserKhalil »

I have copied clean cells from new worksheet and pasted on my range and everything is OK now. It seems that there hidden characters.
Thank you very much.