Thursday, August 11, 2016

AND Function(VBA)

Description

The Microsoft Excel AND function returns TRUE if all conditions are TRUE. It returns FALSE if any of the conditions are FALSE. Please note that the AND function (WS)has a 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.

Applies To

  • 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".