IsBetween - a helpful UDF function

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
There have been many times when I have had to use if statements to determine if a value is between another range of values, and I hated having to create the AND() statements over and over for this, so I finally created an IsBetween UDF that works like a charm, and oddly enough it seems to be able to determine if a single character (a, b, c, etc) is between two other characters!:

Syntax:

=ISBETWEEN([Value],[LowerBound],[UpperBound],[EqualType - (Optional)])

Where Value is the number to determine if it lies between LowerBound and UpperBound. EqualType is a boolean argument where 0, FALSE, or null will return GREATER THAN and LESS THAN (no equal to) and a Number other than 0 or TRUE will return GREATER THAN OR EQUAL TO and LESS THAN OR EQUAL TO.

Code:
Public Function IsBetween(value As Variant, lower As Variant, upper As Variant, Optional equaltype As Boolean) As Boolean
    Select Case equaltype
        Case False
            If value > lower And value < upper Then
                IsBetween = True
            Else
                IsBetween = False
            End If
        Case True
            If value >= lower And value <= upper Then
                IsBetween = True
            Else
                IsBetween = False
            End If
    End Select
End Function

Hope some people are able to find use of this!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I wouldn't name your variable value as Value is a VBA keyword, and UPPER and LOWER are spreadsheet functions.

Your statements with And will return True or False values so you can just assign this to IsBetween

Code:
Public Function IsBetween(testvalue As Variant, lwr As Variant, upr As Variant, Optional equaltype As Boolean) As Boolean
If equaltype Then
    IsBetween = (testvalue >= lwr And testvalue <= upr)
Else
    IsBetween = (testvalue > lwr And testvalue < upr)
End If
End Function
 
Upvote 0
Awesome! I knew there had to be a shorter, sweeter, way to code this. Thank you for the contribution, Hotpepper!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top