In VBA, we use variables to store values that can be used in our code to perform various tasks. The ‘Boolean’ data type is one of the data types available in VBA. It can hold only two values ‘True’ or ‘False’. Let’s learn more about Boolean Data Type with Examples:
Intro to VBA DataType ‘Boolean’
Syntax of Boolean Variable Declaration
To declare a Boolean variable in VBA, we use the following syntax:
Dim variableName As Boolean
The ‘Dim’ keyword is used to declare a variable, followed by the variable name, the keyword ‘As’, and the data type ‘Boolean’.
Storage of Boolean Values
Boolean values are stored as bits in memory, with a value of 0 representing ‘False’ and a value of -1 (or any non-zero value) representing ‘True’. This means that Boolean values take up minimal memory space, making them efficient to use in VBA code.
Range of Boolean Values
As mentioned earlier, Boolean variables can only hold two values ‘True’ or ‘False’. These values are not interchangeable with any other data types, meaning that a Boolean variable cannot hold a numerical value or a string value.
Examples of VBA Codes using Boolean
1. Simple If-Then statement
Dim isRaining As Boolean
isRaining = True
If isRaining Then
MsgBox "It's raining outside."
In this example, we declare a Boolean variable ‘isRaining’ and assign it a value of ‘True’. We then use an If-Then statement to check the value of ‘isRaining’ and display a message if the value is ‘True’.
2. Nested If-Then statement
Dim marks As Integer
marks = 85
Dim isPassing As Boolean
If marks >= 50 Then
If marks < 75 Then
isPassing = True
isPassing = False
If isPassing Then
MsgBox "You have passed the exam."
MsgBox "You have failed the exam."
In this example, we use Boolean variables within a nested If-Then statement to determine if a student has passed an exam. First, we declare an Integer variable 'marks' and assign it a value of 85. Then, we declare a Boolean variable 'isPassing' which will store the value of 'True' or 'False' depending on the value of 'marks'. The nested If-Then statement checks if the value of 'marks' is greater than or equal to 50, and if it is, it further checks if it is less than 75. Depending on the result of this check, the Boolean variable 'isPassing' is assigned a value of 'True' or 'False'. At the end, we use an If-Then statement to display a message based on the value of 'isPassing'.
3. For loop with Boolean condition
Dim i As Integer
Dim isFound As Boolean
For i = 1 To 10
If i = 5 Then
isFound = True
If isFound Then
MsgBox "The value 5 was found in the loop."
MsgBox "The value 5 was not found in the loop."
In this example, we use a For loop to check if the value of the variable 'i' is equal to 5. If it is, we set the value of the Boolean variable 'isFound' to 'True' and exit the loop. At the end, we display a message based on the value of 'isFound'.
4. Select Case statement
Dim day As String
Dim isWeekend As Boolean
day = "Saturday"
Select Case day
Case "Saturday", "Sunday"
isWeekend = True
isWeekend = False
If isWeekend Then
MsgBox "It's the weekend."
MsgBox "It's a weekday."
In this example, we declare a String variable 'day' and assign it a value of "Saturday". Then, we use a Select Case statement to check if the value of 'day' is either "Saturday" or "Sunday". Depending on the value, the Boolean variable 'isWeekend' is assigned a value of 'True' or 'False'. We then display a message based on the value of 'isWeekend'.
5. Boolean function
Function CheckEven(ByVal num As Integer) As Boolean
If num Mod 2 = 0 Then
CheckEven = True
CheckEven = False
In this example, we create a Boolean function called 'CheckEven' which takes in an Integer parameter 'num'. The function checks if 'num' is even by using the Mod (modulo) operator to check for a remainder when divided by 2. If there is no remainder, the function returns a value of 'True', indicating that the number is even. Otherwise, it returns a value of 'False', indicating that the number is odd.
In this blog post, we discussed the VBA data type 'Boolean'. We covered the syntax and storage of Boolean values, as well as examples of VBA codes using the Boolean data type. Boolean variables are useful in writing logic in our VBA code, making our programs more efficient and accurate. As we have seen, there are various ways in which we can use Boolean variables in our code to perform different tasks. By understanding the basics of the Boolean data type and its usage, we can improve our VBA programming skills.