max min VBA

Donjt81

Board Regular
Joined
Jun 11, 2004
Messages
155
How do I get maximum of two numbers in VBA

lets say I have
var1 = 2
var2 = 30

I want to compare the 2 and get the maximum

so maybe something like this. (but i know this doesnt work)

result = max(var1, var2)
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
One way to find the max value:

result = WorksheetFunction.Max(var1, var2)

Edit:

You can find the min value this way as well.

result = WorksheetFunction.Min(var1, var2)
 
Upvote 0
I realize this is a very old thread, but I'm curious -- what is the fastest way to do this? I will be finding the minimum of ~20 number several million times, so I'm looking for the most efficient way to do this.
 
Upvote 0
Just to keep raising the dead...

I found that the standard approach WorksheetFunction was a bit inefficient so I wrote a simple replacement function for it:
Code:
Function Min(ParamArray values() As Variant) As Variant
   Dim minValue, Value As Variant
   minValue = values(0)
   For Each Value In values
       If Value < minValue Then minValue = Value
   Next
   Min = minValue
End Function

What do i mean by inefficient? Processing an array of 60,000 entries using the Levenshtein Distance algorithm to compare a string in the array to another, I cut the time per loop in half.
 
Upvote 0
Just to be complete, for Max, just reverse the inequality comparitor:
Code:
If Value > minValue Then minValue = Value
 
Upvote 0
Just to be complete, for Max, just reverse the inequality comparitor:
Code:
If Value > minValue Then minValue = Value

Thanks for taking the time to fill in a long standing hole left by the geniuses at Microsoft.

I'm sure you meant to change minValue to maxValue ;)

Here's the complete code for both functions:

Code:
Function Min(ParamArray values() As Variant) As Variant
   Dim minValue, Value As Variant
   minValue = values(0)
   For Each Value In values
       If Value < minValue Then minValue = Value
   Next
   Min = minValue
End Function

Function Max(ParamArray values() As Variant) As Variant
   Dim maxValue, Value As Variant
   maxValue = values(0)
   For Each Value In values
       If Value > maxValue Then maxValue = Value
   Next
   Max = maxValue
End Function
 
Upvote 0
How to apply these functions to a Range?
Try This
to apply this =minMax(A1:A10,"min") and hit Enter
=minMax(A1:A10,"max") and hit Enter

VBA Code:
Function minMax(ByVal rRange As Range, MinOrMax As String) As Double
Dim dMin As Double
Dim dMax As Double
Dim lLastRow As Long
Dim ws1 As Worksheet

Set ws1 = ActiveWorkbook.ActiveSheet
lLastRow = ws1.Cells(rRange.Row, rRange.Column).End(xlDown).Row

dMin = ws1.Cells(rRange.Row, rRange.Column).Value
dMax = dMin

For Each cell In rRange.Cells
    If cell.Value < dMin Then dMin = cell.Value
    If cell.Value > dMax Then dMax = cell.Value
Next cell

    If InStr(1, MinOrMax, "min") = 1 Then
        minMax = dMin
    Else
        minMax = dMax
    End If

End Function
 
Upvote 0

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

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