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

Home » VBA » VBA Logical Functions » VBA AND

By Jyoti Singh Leave a Comment

VBA AND Function

AND is a logical function and is also a logical operator which means that if all the conditions provided in this function are fulfilled then only we will have the true result whereas if any one of the condition fails the output is returned to be false, we have inbuilt AND command built-in VBA to use.

VBA AND Function

I hope you have gone through our article on “VBA OR” and “VBA IF OR”. “VBA AND” is just the opposite of the “VBA OR” function. In the “VBA OR” function we needed any one of the supplied logical conditions to be satisfied to get the result as TRUE. But in “VBA AND” function in excel it is just the reverse. In order to get the result of TRUE, all the supplied logical tests need to be satisfied.

Ok, look at the syntax of AND function.

[Logical Test] AND [Logical Test] AND [Logical Test]

AND function 1

In the above, I have two test scores out of 600.

In the result column, I need to get the result as TRUE if the score of both the tests is greater than equal to 250.

Look at the below image of AND in excel.

AND function 1-1

When we apply the logical function AND we got the results. In cell C4 & C5 we got the result as TRUE because Test 1 & Test 2 scores are greater than or equal to 250.

Look at the C6 cell here we have got FALSE even though the score of Test 2 is equal to 250. This is because in Test 1 score is only 179.

Examples of VBA AND Function

Here are the examples of AND function in Excel VBA.

You can download this VBA AND Excel Template here – VBA AND Excel Template

Example #1

Look at the simple example of VBA AND function. For example, we will test the numbers here, whether 25>=20 and 30<=31.

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

Step 1: Declare the variable as String.

Code:

Sub AND_Example1()

  Dim K As String

End Sub

VBA AND Example 1-3

Step 2: For the variable “k” we will assign the value by applying AND function in VBA.

Code:

Sub AND_Example1()

  Dim K As String

  K =

End Sub

VBA AND Example 1-2

Step 3: Supply the first condition as 25>=20.

Code:

Sub AND_Example1()

  Dim K As String

  K = 25 >= 20

End Sub

Example 1-4

Step 4: Now open AND function and supply the second logical test i.e. 30<=29.

Code:

Sub AND_Example1()

  Dim K As String

  K = 25 >= 20 And 30 <= 29

End Sub

Example 1-5

Step 5: Now show the result of the variable “k” in the message box in VBA.

Code:

Sub AND_Example1()

  Dim K As String

  K = 25 >= 20 And 30 <= 29

  MsgBox K

End Sub

Example 1-6

Run the macro and see what the result is.

VBA AND Example 1-7

We got the result as FALSE because we out of applied two conditions first condition 25>=20, this condition is satisfied so the result is TRUE and second condition 30<=29 this is not satisfied result is FALSE. In order to get the result as TRUE both the conditions should be satisfied.

Example #2

Now I will change the logical test to “100>95 AND 100<200”

Code:

Sub AND_Example2()

  Dim k As String

  k = 100 > 95 And 100 < 200

  MsgBox k

End Sub

Run the code to see the result.

VBA AND Example 2

Here we got TRUE as the result because

1st Logical Test: 100 > 95 = TRUE

2nd Logical Test: 100 < 200 = TRUE

Since we got TRUE result for both the logical tests our final result as TRUE.

Example #3

Now we will see data from the worksheet. Use the data that we have used to show the example of excel AND function.

Example 3

Here the condition is Test 1 Score >= 250 AND Test 2 Score >= 250.

Since we have more than one cell of data we need to use loops to avoid writing unnecessary and time-consuming lines of codes. I have written the below code for you, formula and logical is the same only thing is I have used “VBA For Next Loop”.

Code:

Sub AND_Example3()

  Dim k As Integer

  For k = 2 To 6
   Cells(k, 3).Value = Cells(k, 1) >= 250 And Cells(k, 2) >= 250
  Next k

End Sub

This will give the result exactly the same as our worksheet function but we will not get any formulas, we get only results.

VBA AND Example 3-1

Like this, we can apply AND logical function in VBA & in excel to test multiple conditions which are all should be TRUE to arrive the desired results.

This works completely opposite of “VBA OR” function, where OR requires any one of the supplied condition to be TRUE to arrive the results but AND requires 100% result in logical test to arrive the results.

Recommended Articles

This has been a guide to VBA AND Function. Here we learn how to use AND logical operator in Excel VBA along with practical examples and download excel template. You may also have a look at other articles related to Excel VBA –

  • Switch Case in Excel VBA
  • Examples of Wait Function in VBA
  • VBA DatePart Function Examples
  • VBA Activate Method
  • FileDialog in VBA
  • Data Types in VBA
  • Count Numerical Values in VBA
  • Find Null Values in VBA
  • Find Last Used Row using VBA
0 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 AND 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