Saturday, August 13, 2016

How to use the OR Function (VBA)

Description

The Microsoft Excel OR function returns TRUE if any of the conditions are TRUE. Otherwise, it returns FALSE.
TIP: The OR function when used as a worksheet function has a different syntax than the VBA version.

Syntax

The syntax for the OR function in Microsoft Excel is:
condition1 Or condition2 [... Or 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)

The OR function with this syntax can only be used in VBA code in Microsoft Excel.
Let's look at some Excel OR function examples and explore how to use the OR function in Excel VBA code.
This first example combines the OR function with the IF Statement in VBA:
If LWebsite = "TechOnTheNet.com" Or LCount > 25 Then
   LResult = "Great"
Else
   LResult = "Fair"
End If
This would set the LResult variable to the string value "Great" if either LWebsite was "TechOnTheNet.com" or LCount > 25. Otherwise, it would set the LResult variable to the string value "Fair".
You can use the OR function with the AND 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".