REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

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."
End If

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
    Else
        isPassing = False
    End If
End If
If isPassing Then
     MsgBox "You have passed the exam."
Else
     MsgBox "You have failed the exam."
End If

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
        Exit For
    End If
Next i
If isFound Then
     MsgBox "The value 5 was found in the loop."
Else
     MsgBox "The value 5 was not found in the loop."
End If

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
    Case Else
        isWeekend = False
End Select
If isWeekend Then
     MsgBox "It's the weekend."
Else
     MsgBox "It's a weekday."
End If

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
    Else
        CheckEven = False
    End If
End Function

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.

Conclusion

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.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA Data TypesLast Updated: September 23, 2023

Leave A Comment