Excel

Calculate Max Between Two Numbers in a Range of Data

Ease of Use

Easy

Version tested with

2000, 2002 

Submitted by:

Jacob Hilderbrand

Description:

This User Defined Function will calculate the maximum value between specified upper and lower limits in a specified range of data. 

Discussion:

You need to calculate the Maximum value of a range, but you want to set the lower and upper numbers for that range. For example you want the maximum value appearing in the range between 1000 and 2000. 

Code:

instructions for use

			

Option Explicit Function MaxBetween(Rng1 As Range, Lower As Double, Upper As Double) Dim Cell As Range Dim Max As Double For Each Cell In Rng1 If Cell.Value >= Lower And Cell.Value <= Upper Then Max = Application.WorksheetFunction.Max(Max, Cell.Value) End If Next MaxBetween = Max End Function

How to use:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Insert | Module.
  4. Paste the code there.
  5. Close the VBE (Alt + Q or press the X in the top right corner).
 

Test the code:

  1. Enter data in the Range A1:A100, e.g in cell A1 type: =2000*RAND() and copy down.
  2. In a cell type: =MaxBetween(A1:A100,1000,2000)
 

Sample File:

No Attachment 

Approved by Jacob Hilderbrand


This entry has been viewed 137 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express