CBool Function

Returns an expression that has been converted to a Variant of subtype Boolean.

Syntax

CBool(expression)

Remarks

The expression argument is any valid expression.

If expression is zero, False is returned; otherwise, True is returned. If an expression can't be interpreted as a numeric value, a run-time error occurs.

The following example uses the CBool function to convert an expression to a Boolean. If the expression evaluates to a nonzero value, CBool returns True; otherwise, it returns False.

The following example illustrates the use of the CBool function:

Example 1: Comparing two equal numbers

Dim A, B
A = 5
B = 5
Check = CBool(A = B)   'Converts the expression (A = B) to Boolean
'Output: True

Example 2: Testing a variable with non-zero value

Dim A
A = 1
Check = CBool(A)   'Converts the expression (A) to Boolean
'Output: True

Example 3: Testing a variable with zero value

Dim A
A = 0
Check = CBool(A)   'Converts the expression (A) to Boolean
'Output: False

Example 4:

Dim strValue
strValue = "Hello"
Check = CBool(Len(strValue) > 0)   'Converts the result of Len(strValue) > 0 to Boolean
'Output: True

Example 5: Empty string

Dim strValue
strValue = ""
Check = CBool(Len(strValue) > 0)   'Converts the result of Len(strValue) > 0 to Boolean
'Output: False

Example 6: Using CBool in Conditional Statements

Dim value
value = 10
If CBool(value) Then 'It will enter in to IF part because value is non-zero
'Statements based on use case when value is non-zero
Else
'Statements based on use case when value is zero
End If