totn Excel Functions

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

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

Description

The Microsoft Excel OR function returns TRUE if any of the conditions are TRUE. Otherwise, it returns FALSE.

The OR 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 OR function (WS) page if you are looking for the worksheet version of the OR function as it has a very different syntax.

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.

Returns

The OR function returns TRUE if any of the conditions are TRUE.
The OR function returns FALSE if all 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)

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