totn Excel Functions

MS Excel: How to use the AND Function (VBA)

This Excel tutorial explains how to use the Excel AND function (in VBA) with syntax and examples.

Description

The Microsoft Excel AND function returns TRUE if all conditions are TRUE. It returns FALSE if any of the conditions are FALSE.

The AND function is a built-in function in Excel that is categorized as a Logical Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

Please read our AND function (WS) page if you are looking for the worksheet version of the AND function as it has a very different syntax.

Syntax

The syntax for the AND function in Microsoft Excel is:

condition1 And condition2 [... And condition_n]

Parameters or Arguments

condition1, condition2, ... condition_n
Expressions that you want to test that can either be TRUE or FALSE.

Returns

The AND function returns TRUE if all conditions are TRUE.
The AND function returns FALSE if any of the conditions are FALSE.

Applies To

  • Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

Let's look at some Excel AND function examples and explore how to use the AND function in Excel VBA code.

This first example combines the AND function with the IF Statement in VBA code:

If LWebsite = "TechOnTheNet.com" And LPages <= 10 Then
   LBandwidth = "Low"
Else
   LBandwidth = "High"
End If

This would set the LBandwidth variable to the string value "Low" if both LWebsite was "TechOnTheNet.com" and LPages <= 10. Otherwise, it would set the LBandwidth variable to the string value "High".

This second example uses the AND function with the OR function in VBA, for example:

If (LWebsite = "TechOnTheNet.com" Or LWebsite = "CheckYourMath.com") And LPages <= 10 Then
   LBandwidth = "Low"
Else
   LBandwidth = "High"
End If

This would set the LBandwidth variable to the string value "Low" if LWebsite was either "TechOnTheNet.com" or "CheckYourMath.com" and LPages <= 10. Otherwise, it would set the LBandwidth variable to the string value "High".