EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Logical Functions VBA AND
 

VBA AND

Madhuri Thakur
Article byMadhuri Thakur

VBA AND

Excel VBA AND Function

VBA AND function checks the provided statement whether it is true or false. AND function also evaluates the conditions with one another. If both the statements are true, the expression becomes true. Commonly this is associated with different decision-making loops to express the conditions. Comparing to all other logical operators AND returns true only if all the supplied conditions are true.

 

 

When you want to evaluate more than one expression and take decisions according to that then logical operators are the best option. AND operators connect two or more conditions together and evaluate the entire statement as a single expression. If all the statement is true, then the total expression returns true. If any expression evaluates false, then the entire statement will return false.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to Use the AND Function in Excel VBA?

The logical AND can be used along with comparison operators, arithmetic operators, and text, etc. Any number of statements can be connected using AND. The logic behind AND operator is as follows.

Condition 1 Condition 2 Result
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE

When more than two conditions are used, each condition should return true to make the entire expression true.

Syntax of VBA AND:

Format of VBA AND is expressed as,

[Condition1] And [Condition 2] And [Condition 3] And………. [Condition n]

  • [Condition 1 to Condition n] can be a statement expressed using text and any operators.
You can download this VBA AND Excel Template here – VBA AND Excel Template

Example #1

In this example, you can learn how to use AND function along with comparison operators. You have the marks scored by a student in a different subject. If the total comes more than 80 and 22 marks on the main subject the student got passed in the exam else fail. For this, follow the below steps:

Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.

Insert Module

Step 2: Create a function named result in VBA to do the calculation and find the total mark of all subjects.

Code:

Private Sub result()

End Sub

VBA AND Example1-2

Step 3: Give the marks for different subjects. a, b, c, d are different subjects and the marks scored for each subject is as below. a is the main subject and s is the sum of all subjects a, b, c, d.

Code:

Private Sub result()

a = 25
b = 20
c = 20
d = 20
s = a + b + c + d

End Sub

VBA AND Example1-3

Step 4: Now express the conditions as two statements and connect with AND function. IF …Else loop is used to execute the two different results according to the evaluation of the expression.

Code:

Private Sub result()

a = 25
b = 20
c = 20
d = 20
s = a + b + c + d
If s > 80 And a > 22 Then
Else
End If

End Sub

VBA AND Example1-4

Step 5: Set the message to show according to the condition valuation. Where both conditions are true the entire expression becomes true and the true section of IF… Else loop will execute. Else the control will move to the false section.

Code:

Private Sub result()

a = 25
b = 20
c = 20
d = 20
s = a + b + c + d
If s > 80 And a > 22 Then
MsgBox "Student got passed the exam"
Else
MsgBox "Failed"
End If

End Sub

IF-Else loop Example1-5

So if the sum is greater than 80 and the mark for the main subject ‘a’ is greater than 22 then the message “student got passed the exam” will show else fail is the result.

Step 6: Run this code by hitting the F5 or Run button which is placed on the topmost ribbon of VBE. The marks scored in the main subject is greater than 22 and the sum of all subject is greater than 80.

VBA AND Example1-6

Here both the condition is true so the entire logical AND expression become true and the message in true section of IF… Else loop will be executed.

Example #2

There is no limit for giving the number of conditions with VBA AND. You can use ‘n’ number of conditions and VBA AND operator within a single expression. From an employee database, you have the attendance of a particular employee. You have the number of days that he was present in the office for the past 5 months. If the employee has 25 or more than 25 days’ attendance for every month, he is eligible for a bonus. For this, follow the below steps:

Step 1: In the same module let us start with another subprocedure.

Code:

Private Sub bonus()

End Sub

VBA AND Example2-1

Step 2: To check the above conditions, start with a function ‘bonus’ and attendance for each month.

Private Sub bonus()

jan = 25
feb = 24
mar = 25
apr = 25

End Sub

VBA AND Example2-2

Step 3: Now, check each month’s attendance to confirm whether it is greater than or equal to 25. So multiple ‘AND’ operators are used to check the condition. The value of the entire expression is assigned to ‘b’ which returns a Boolean value.

Code:

Private Sub bonus()

jan = 25
feb = 24
mar = 25
apr = 25
b = jan >= 25 And feb >= 25 And mar >= 25 And apr >= 25

End Sub

VBA AND Example2-3

Step 4: According to this Boolean value, you can a loop. The value of ‘b’ should be true or false.

Code:

Private Sub bonus()

jan = 25
feb = 24
mar = 25
apr = 25
b = jan >= 25 And feb >= 25 And mar >= 25 And apr >= 25
If b = "True" Then
MsgBox " Employee eligible for bonus"
End If
If b = "False" Then
MsgBox "Employee does not meet the criteria"
End If

End Sub

Loop Example2-4

Two IF loops are set to execute according to the Boolean value of b. Here apart from Feb all month’s attendance is greater than or equal to 25. But even a single statement evaluates false the entire expression becomes false. ‘AND’ function returns false and the value of ‘b’ becomes false.

Step 5: So the second IF loop will be executed and the message within this will display as “Employee does not meet the criteria.

VBA AND Example2-5

Here expression is evaluated as follows. First and last two conditions are true and the second condition is false. So while evaluating the entire expression, it becomes false.

Example #3

VBA AND to Evaluate User Credentials. Create a sign-in form using forms and controls in VBA which is available in the code window. For this, follow the below steps:

Step 1: Now Insert a new UserForm inside Visual Basic Editor (VBE). Click on Insert tab > select UserForm. Design a window using Toolbox with a user name, password and a sign-in button.

UserForm Example3-1

Step 2: Click the Sign-in button using the AND operator. Once the credentials are provided check the text value comes to both username and password textboxes, Textbox1 and Textbox 2. If the username is “Tutorial” and password is “Edcba1A45” then the credentials are correct and the user will be able to sign in and a welcome message will be displayed. Else error message is displayed.

Code:

Private Sub CommandButton1_Click()

If (TextBox1.Text = "Tutorial") And (TextBox2.Text = "Edcba1A45") Then
MsgBox "Welcome to your account'"
Else
MsgBox "Oops username or password is incorrect"
End If

End Sub

VBA AND Example3-2

Step 3: Both conditions are expressed in an IF loop. AND operator evaluates these both conditions. Run the form using the run button and give username and password in the text field.

User Credentials Example3-3

User Credentials3-3

Since the username is “Tutorial” and password is “Edcba1A45” the AND operator returns a true and true block of IF loop will execute.

Things to Remember

  • The logical AND function will always return a Boolean value true or false
  • Commonly use with decision-making loops.
  • Helps to compare ‘n’ number of expressions at a time by connecting with logical AND
  • The entire statement returns true only if each statement is true.

Recommended Articles

This is a guide to the VBA AND. Here we discuss how to Use the AND Function in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA PasteSpecial
  2. VBA Dynamic Array
  3. VBA ReDim Array
  4. VBA SubString

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

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
EDUCBA

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

EDUCBA

Download VBA AND Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW