VBA CBool Function in Excel is categorizes as a Data Type Conversion function. It is a built-in function in Excel VBA. This VBA CBool function converts an expression to a Boolean data type.
This function use in either procedure or function in a VBA editor window in Excel. We can use this VBA CBool Function in any number of times in any number of procedures or functions. In the following section you learn many topics. Like what is the syntax and parameters of the CBool function, where we can use this VBA CBool Function and it’s real-time examples.
- Overview
- Syntax of VBA CBool Function
- Parameters or Arguments
- Where we can apply or use VBA CBool Function?
- Example 1: Convert a Value(0) to Boolean
- Example 2: Convert a Value(-10) to Boolean
- Example 3: Convert a String(True) to Boolean
- Example 4: Convert a String(False) to Boolean
- Example 5: Convert a String(“Example”) to Boolean(Overflow error)
- Instructions to Run VBA Macro Code
- Other Useful Resources
Syntax of VBA CBool Function:
The syntax of the CBool Function in VBA is
CBool(Expression)
The CBool function returns a Boolean data type value. The value is either True or False.
Note:
1. Returns True: For Non zero values and True string.
2. Returns False: For zero values and False string.
Parameters or Arguments:
The CBool function has one argument in Excel VBA.
where
Expression:It is a mandatory argument. An expression argument represents a numeric or string value. It is used to convert the value or string to a Boolean value. If the expression is a string, returns an error.
Where we can apply or use VBA CBool Function?
We can use this CBool Function in VBA MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Example 1: Convert a Value(0) to Boolean
Here is a simple example of the VBA CBool function. This below example converts a value ‘0’ to a boolean. It returns False as output.
'Convert a Value(0) to Boolean Sub VBA_CBool_Function_Ex1() 'Variable declaration Dim iValue As Integer Dim bResult As Boolean iValue = 0 bResult = CBool(iValue) MsgBox "The value(0) in Boolean is : " & bResult, vbInformation, "VBA CBool Function" End Sub
Output: Here is the screen shot of the first example output.
Example 2: Convert a Value(-10) to Boolean
Here is a simple example of the VBA CBool function. This below example converts a value ‘-10’ to a boolean. It returns True as output.
'Convert a Value(-10) to Boolean Sub VBA_CBool_Function_Ex2() 'Variable declaration Dim iValue As Integer Dim bResult As Boolean iValue = -10 bResult = CBool(iValue) MsgBox "The value(-10) in Boolean is : " & bResult, vbInformation, "VBA CBool Function" End Sub
Output: Here is the screen shot of the second example output.
Example 3: Convert a String(True) to Boolean
Here is a simple example of the VBA CBool function. This below example function converts given expression ‘True’ to a Boolean value.
'Convert a String(True) to Boolean Sub VBA_CBool_Function_Ex3() 'Variable declaration Dim iValue Dim bResult As Boolean iValue = True bResult = CBool(iValue) MsgBox "The string(True) in Boolean is : " & bResult, vbInformation, "VBA CBool Function" End Sub
Output: Here is the screen shot of the third example output.
Example 4: Convert a String(False) to Boolean
Here is a simple example of the VBA CBool function. This below example function converts given expression ‘False’ to a Boolean value.
'Convert a String(False) to Boolean Sub VBA_CBool_Function_Ex4() 'Variable declaration Dim iValue Dim bResult As Boolean iValue = False bResult = CBool(iValue) MsgBox "The string(False) in Boolean is : " & bResult, vbInformation, "VBA CBool Function" End Sub
Output: Here is the screen shot of the fourth example output.
Example 5: Convert a String(“Example”) to Boolean(Type mismatch)
Here is a simple example of the VBA CBool function. This below example converts an expression to a boolean value. It returns type mismatch error. Because, we can’t convert string value to Boolean.
'Convert a String("Example") to Boolean(Type mismatch) Sub VBA_CBool_Function_Ex5() 'Variable declaration Dim iValue As String Dim bResult As Boolean iValue = "Example" bResult = CBool(iValue) MsgBox "The String('Example') in Boolean is : " & bResult, vbInformation, "VBA CBool Function" End Sub
Output: Here is the screen shot of the fifth example output.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers