VBA: Empty and "" ?

vuminhduc

New Member
Joined
Apr 15, 2013
Messages
3
Please look at these lines of code:

Sub test()
x = Application.WorksheetFunction.CountIf([a1:a2], [a1])
y = Application.WorksheetFunction.CountIf([a1:a2], "")
z = [a1] = ""
End Sub

Basically there is nothing in cell a1 and a2.
Pressing F5 returns x=0, y=2 and z = True. How can it be ? Should x must be equal to y as z is True?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I think the reason is this:


Code:
Syntax
COUNTIF(range, criteria)

range    Required. One or more cells to count, including numbers or names, arrays, or references that contain numbers. [B][U]Blank and text values are ignored.[/U][/B]

criteria    Required. [U]A number, expression, cell reference,[/U] [B][U]or text string[/U][/B] that defines which cells will be counted. For example, criteria can be expressed as 32, ">32", B4, "apples", or "32".


I think a "blank cell" is different from a cell that contains an "empty text string", at least from the point of view of the CountIf function...

So Excel detects no criteria, since it's a blank and blanks are not a usable criteria.
 
Last edited:
Upvote 0
This is the code you where looking for:

Code:
Sub vuminhduc()

Dim cll As Range
Dim x As Long
Dim y As Long
Dim z As String

For Each cll In Range("A1:A2")
    If cll.Value = Range("A1").Value Then
    x = x + 1
    End If
    
    If cll.Value = "" Then
    y = y + 1
    End If
Next cll

If Range("A1").Value = "" Then
z = True
Else
z = False
End If

MsgBox "X: " & x & vbNewLine & "Y: " & y & vbNewLine & "Z: " & z

End Sub
 
Upvote 0
I think the reason is this:


Code:
Syntax
COUNTIF(range, criteria)

range    Required. One or more cells to count, including numbers or names, arrays, or references that contain numbers. [B][U]Blank and text values are ignored.[/U][/B]

criteria    Required. [U]A number, expression, cell reference,[/U] [B][U]or text string[/U][/B] that defines which cells will be counted. For example, criteria can be expressed as 32, ">32", B4, "apples", or "32".


I think a "blank cell" is different from a cell that contains an "empty text string", at least from the point of view of the CountIf function...

So Excel detects no criteria, since it's a blank and blanks are not a usable criteria.

Thanks for replying, but so why z is true ?
 
Upvote 0
Because (in z) an "empty text string" is a usable criteria; and a "blank cell" = "empty text string" (when comparing) (TRUE).

It appears that the problem is that the countif function doesn't accept (well it accepts it, but doesn't use it; I agree, this is weird, it should return an #ERROR) a "blank cell" as criteria, in that particular case a "blank cell" is different from an "empty text string" but only applies when "blank cell" is being use as a criteria.

Be aware that I'm theorizing here, I'm just intrigue by just question...


Also, you may change your first formula for this:
=IF(ISBLANK(A1),COUNTIF(A1:A2,""),COUNTIF(A1:A2,A1))
That way you'll get the correct answer.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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