Excel: Get Maximum Number Between 2 Numbers

< Back to Search results

 Category: [Excel]  Demo Available 

Excel: Get Maximum Number Between 2 Numbers


Got any Excel/VBA Questions? Free Excel Help

This Custom Function will extract the maximum number between any two specified numbers. If used on large ranges of data, it will slow down Excel's calculation.

The Code

Function GetMaxBetween(rCells As Range, MinNum, MaxNum)

Dim rRange As Range

Dim vMax

Dim aryNums()

Dim i As Integer

ReDim aryNums(rCells.Count)

    For Each rRange In rCells

     vMax = rRange

        Select Case vMax

           Case MinNum + 0.01 To MaxNum - 0.01

              aryNums(i) = vMax

              i = i + 1

           Case Else

               GetMaxBetween = 0

           End Select

    Next rRange


    GetMaxBetween = WorksheetFunction.Max(aryNums)

End Function

To use this UDF push Alt+F11 and go Insert>Module and paste in the code. Push Alt+Q and save. The Function will appear under "User Defined" in the Paste Function dialog box (Shift+F3). Use the Function in any cell as shown below. 

This would return the highest number in the range A1:A100 that is less than 10, but greater than 9. Only accurate up to 0.01


See also:

Used to Loop Through a Collection or Array
For Loop
For Loop Step
Convert Excel Formulas from Relative to Absolute
Custom Excel Formulas - User Defined Functions/Formulas
Parse/Extract File Name from Full File Name and Path


See also Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions.


Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.


stars (0 Reviews)