Return to VBA Code Examples

VBA Boolean Data Type (Dim Variable)

Boolean Variable Type

The VBA Boolean data type is used to store True or False values.   True can also be represented by 1 and False by 0.

To declare an Boolean variable, you use the Dim Statement (short for Dimension):

Then, to assign a value to a variable, you simply use the equal sign:

When you put this in a procedure, it could look like this:

If you run the code above, the following message box will be shown.

vba boolean msgbox example

Declare Boolean Variable at Module or Global Level

In the previous example, we’ve declared the Boolean variable within a procedure. Variables declared with a procedure can only be used within that procedure.

vba boolean procedure declare

Instead, you can declare Boolean variables at the module or global level.

Module Level

You declare Module level variables at the top of code modules with the Dim statement.

vba boolean module declare

These variables can be used with any procedure in that code module.

Global Level

You also declare Global level variables at the top of code modules. However, instead of using the Dim statement, you would use the Public statement to indicate that the Boolean variable is available to be used throughout your VBA Project.

vba boolean global declare

If you were to declare the Boolean variable at a module level and then try to use it in a different module, you would get an error.

vba boolean module declare error

However, if you had used the Public keyword to declare the Boolean variable, the error would not occur and the procedure would run perfectly.

Using a Boolean Variable

You use the Boolean variable in logical comparison.  These are often used with If statements to test if a condition is True or False as per the example above, or in a line of code to apply a logical test – perhaps to see if one value is greater than another.

If you run the code above, you will get the following message box.

vba boolean msgbox false

because of course 45 is not greater than 68!

Using Boolean Operators

As Boolean variables are used in logical comparison, we can use the logical operators AND and OR to test to see if more than one condition is true or false.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

Using the AND operator

We can use the AND function to see if BOTH conditions are met.

or we could run the same test using an If Statement:

Both examples above would return FALSE due to the fact that 10 is NOT greater than 13 – and BOTH conditions have to be True for the Boolean to be True.

Using the OR operator

We can use the OR function to see if ONE OF the conditions is met.

or we could run the same test using an If Statement:

These examples would return TRUE due to the fact that 10 is NOT greater than 13 BUT 15 IS greater than 12  – and ONLY ONE condition has to be True for the Boolean to be True.

Using If statements allows us to use more logical operators

Using the NOT operator

We can also use the NOT operator with the Boolean variable.  The NOT operator negates the value of the condition – so if a condition is true, the NOT operator will return false.

VBA Programming | Code Generator does work for you!

Using the Xor Logical Operator

The Xor logical operator is used to compare two or more conditions.  If one of the conditions is true, it will return TRUE.  If there are 2 conditions, and NEITHER are true or BOTH are true, it will return FALSE.

In the above example, as ONE of the conditions is TRUE, the message box will return TRUE.

However, in the example above, as BOTH conditions are true, the message box will return FALSE.

and finally, as both conditions are FALSE, the message box will also return FALSE.