VBA CBool function

CBool Function Description

The VBA CBool function converts an expression (variable) to Boolean data type.

Syntax

The syntax for the CBool function in VBA is:

CBool( expression )

Parameters

expression
An expression / variable that is to be converted to Boolean data type.

Other Notes

If the expression / variable can’t be converted to Boolean the function with return Error code 13: Type mismatch.
The CBool function will convert:

  • Numbers – Non-zero value will be converted to True, zero values are converted to False.
  • Strings – “true” string will convert to True, “false” will be converted to False.

Example usage

The CBool function can be used in VBA code. Let’s look at some VBA CBool function examples:

CBool 1 
'Result: True

CBool 0 
'Result: False

CBool 21 
'Result: True

CBool "true"
'Result: True

CBool "false"
'Result: False

Dim x, y
x = 1: y = 10
CBool x=y
'Result: False
x = 22: y = 22
CBool x = y
'Result: True