• Skip to primary navigation
  • Skip to main content
  • Skip to footer
WallStreetMojo

Wallstreet Mojo

Wallstreet Mojo

MENUMENU
  • Resources
        • Excel

          • Excel Functions
          • Excel Tools
          • Excel Tips
        • Excel
        • Financial Functions Excel

          • NPV in Excel
          • IRR in excel
          • PV in Excel
        • Financial-Functions-Excel
        • Lookup Functions Excel

          • VLOOKUP
          • HLOOKUP
          • Index Function
        • Lookup-Functions-in-Excel
        • Excel Charts

          • Pareto Chart in Excel
          • Gannt Chart in Excel
          • Waterfall Chart in Excel
        • Excel-Charts
        • VBA

          • VBA Left Function
          • VBA Paste Special
          • VBA Worksheet Function
        • VBA
        • Others

          • Resources (A to Z)
          • Financial Modeling
          • Equity Research
          • Private Equity
          • Corporate Finance
          • Financial Statement Analysis
  • Free Courses
  • All Courses
        • Certification Courses

          Excel Course certificate
        • Excel VBA All in One Bundle

          Excel-VBA-Certification-Course
        • Excel Data Analysis Course

          Excel-Data-Analysis-Course
        • VBA Macros Course

          VBA-Training-Course
        • Others

          • Basic Excel Training
          • Advanced Excel Course
          • Tableau Certification Course
          • Excel for Finance Course

          • Excel for Marketers Course
          • Excel for HR Managers
          • Excel for Power Users
          • View All
  • Excel VBA All in One Bundle
  • Login

VBA Boolean

Home » VBA » VBA Logical Functions » VBA Boolean

By Sharmila Reddy Leave a Comment

VBA Boolean

Excel VBA Boolean Operator

Excel VBA Boolean is a kind of data type that holds either TRUE or FALSE. In VBA or Excel, TRUE and FALSE are logical values. Just remember that IF condition in excel it works based on these two results. IF condition tests the condition we supply and returns TRUE if the logical test is TRUE and if the logical test is FALSE it will return FALSE.

As I told 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.

Working with Boolean Data Type in VBA Programming Language

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

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 sub procedure by naming the macro name.

Code:

Sub Boolean_Example1()

End Sub

VBA Boolean Example 1

Step 2: Declare the variable as BOOLEAN.

Code:

Sub Boolean_Example1()

  Dim MyResult As Boolean

End Sub

VBA Boolean Example 1-1

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

Code:

Sub Boolean_Example1()

  Dim MyResult As Boolean

  MyResult = 25 > 20

End Sub

VBABoolean Example 1-2

Step 4: Now show the result in a message box in VBA.

Code:

Sub Boolean_Example1()

  Dim MyResult As Boolean

  MyResult = 25 > 20

  MsgBox MyResult

End Sub

VBA Boolean Example 1-3

Now run the excel macro through F5 key or manually and see the result.

Popular Course in this category
Cyber Monday Sale
VBA Training (3 Courses, 12+ Projects) 3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
4.6 (247 ratings)
Course Price

View Course

Related Courses

VBA Boolean Example 1-4

Ok, we got the result as TRUE because number 25 is greater than the number 20, so 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 an example look at the below code.

Code:

Sub Boolean_Example2()

  Dim BooleanResult As Boolean

  BooleanResult = "Hello"

  MsgBox BooleanResult

End Sub

VBABoolean Example 2

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 F5 key or manually, I will get the type mismatch error because of data type mismatch value.

VBA Boolean Example 2-1

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 an example look at the below code in VBA.

Code:

Sub Boolean_Example3()

    Dim BooleanResult As Boolean

    BooleanResult = 1

    MsgBox BooleanResult

End Sub

VBABoolean Example 3

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

VBA Boolean Example 3-1

Now, look at the below code.

Code:

Sub Boolean_Example3()

    Dim BooleanResult As Boolean

    BooleanResult = 0

    MsgBox BooleanResult

End Sub

VBABoolean Example 3-2

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

VBA Boolean Example 3-3

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 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

VBABoolean Example 4

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

Recommended Articles

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 –

  • VBA Remove Duplicates
  • How to use Split Function in VBA?
  • VBA Tutorial | Macros in Excel
  • VBA End Function
  • Call Sub Method in Excel
  • Union in VBA
  • Use IF OR Function in VBA
  • Using IFERROR in VBA
  • Use Do Until Loop in VBA
7 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>

Filed Under: VBA, VBA Logical Functions

Reader Interactions
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Footer
COMPANY
About
Reviews
Blog
Contact
Privacy
Terms of Service
FREE COURSES
Free Finance Online Course
Free Accounting Online Course
Free Online Excel Course
Free VBA Course
Free Investment Banking Course
Free Financial Modeling Course
Free Ratio Analysis Course

CERTIFICATION COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Business Valuation Course
Equity Research Course
CFA Level 1 Course
CFA Level 2 Course
Venture Capital Course
Microsoft Excel Course
VBA Macros Course
Accounting Course
Advanced Excel Course
Fixed Income Course
RESOURCES
Investment Banking
Financial Modeling
Equity Research
Private Equity
Excel
Books
Certifications
Accounting
Asset Management
Risk Management

Copyright © 2019. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

By continuing above step, you agree to our Terms of Use and Privacy Policy.
WallStreetMojo

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

By continuing above step, you agree to our Terms of Use and Privacy Policy.

* Please provide your correct email id. Login details for this Free course will be emailed to you

WallStreetMojo

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

By continuing above step, you agree to our Terms of Use and Privacy Policy.

* Please provide your correct email id. Login details for this Free course will be emailed to you

WallStreetMojo

Free Investment Banking Course

IB Excel Templates, Accounting, Valuation, Financial Modeling, Video Tutorials

By continuing above step, you agree to our Terms of Use and Privacy Policy.

* Please provide your correct email id. Login details for this Free course will be emailed to you

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.

* Please provide your correct email id. Login details for this Free course will be emailed to you

WallStreetMojo

Download VBA Boolean Data Type Excel Template

By continuing above step, you agree to our Terms of Use and Privacy Policy.

Limited Period Offer - VBA Training Course (6 courses, 35+ hours video) View More