VBA Boolean Article byJeevan A Y Excel VBA Boolean Operator

Boolean is a data type and it is also an inbuilt data type in VBA, this data type is used for logical references or logical variables because the value this data type holds is either TRUE or FALSE which is used for logical comparison, the declaration of this data type is similar to all the other data types.

As I told, the Boolean data type can hold either TRUE or FALSE as the data, but it can also hold number 1 as TRUE and number 0 as FALSE. So, TRUE is represented by 1, and FALSE is represented by 0. When we declare the variable as BOOLEAN, it occupies 2 bytes of computer memory.

For eg:
Source: VBA Boolean (wallstreetmojo.com)

Working with Boolean Data Type in VBA Programming Language

Now let’s see the example of setting Boolean Operator values to variables .

You can download this VBA Boolean Data Type Excel Template here – VBA Boolean Data Type Excel Template

Follow the below steps to have a fair bit of knowledge on Boolean data types in VBA.

Step 1: First, start the subprocedure by naming the macro name.

Code:

Sub Boolean_Example1()

End Sub

Step 2: Declare the variable as BOOLEAN.

Code:

Sub Boolean_Example1()

Dim MyResult As Boolean

End Sub

Step 3: Now, for the variable “MyResult,” apply the simple as 25 > 20.

Code:

Sub Boolean_Example1()

Dim MyResult As Boolean

MyResult = 25 > 20

End Sub

Code:

Sub Boolean_Example1()

Dim MyResult As Boolean

MyResult = 25 > 20

MsgBox MyResult

End Sub

Ok, we got the result as TRUE because the number 25 is greater than the number 20, so the logical test is correct, and the result is TRUE.

This is the basic structure of VBA Boolean Datatypes.

Boolean Data Type Cannot Hold Other than TRUE or FALSE.

VBA Boolean is a logical data type; it holds TURE or FALSE. Anything other than TRUE or FALSE will show an error message as “Type Mismatch” in VBA.

For example, look at the below code.

Code:

Sub Boolean_Example2()

Dim BooleanResult As Boolean

BooleanResult = "Hello"

MsgBox BooleanResult

End Sub

In the above code, I have declared the variable “BooleanResult” as Boolean.

Dim BooleanResult As Boolean

In the next line, I have assigned the value to the declared variable as “Hello.”

BooleanResult = "Hello"

I have declared the variable as Boolean, but I have assigned the value as “Hello,” which is other than logical values i.e., either TRUE or FALSE.

When I run this code using the F5 key or manually, I will get the type mismatch error because of data type mismatch value.

All the Numbers are TRUE, and Zero is FALSE

As I told, TRUE is represented by number 1, and FALSE is represented by 0. For example, look at the below code in VBA.

Code:

Sub Boolean_Example3()

Dim BooleanResult As Boolean

BooleanResult = 1

MsgBox BooleanResult

End Sub

I have assigned the value to the variable as 1, and this will show the result as TRUE.

Now, look at the below code.

Code:

Sub Boolean_Example3()

Dim BooleanResult As Boolean

BooleanResult = 0

MsgBox BooleanResult

End Sub

In this code, I have assigned the value to the variable as 0, and this will show the result as FALSE.

Not only 1 or 0 we, but any number assigned to the variable except zero is also treated as TRUE, and only zero will be treated as 1.

VBA Boolean Operator with IF Condition

Since the Boolean data type can hold only logical values, it is best suited to use with IF condition in VBA.

Code:

Sub Boolean_Example2()

Dim Number1 As Integer
Dim Number2 As Integer

Number1 = 80
Number2 = 75

If Number1 >= Number2 Then
MsgBox True
Else
MsgBox False
End If

End Sub

Like this, we can use Excel VBA Boolean data types to store the results as either TRUE or FALSE.

This has been a guide to VBA Boolean Operator. Here we learn how to use Boolean Data type in Excel VBA, which gives the logical output i.e., TRUE or FALSE, along with examples & downloadable templates. Below are some useful articles related to VBA –

• 3 Courses
• 12 Hands-on Projects
• 43+ Hours