Find variable with the highest value

  • Thread starter Thread starter MJKelly
  • Start date Start date
M

MJKelly

Hi, I have 5 variables containing integers. How can I use vba to
identify the variable with the highest value?

Thanks
Matt
 
There is no magic here...

dim lngMax as long

lngMax = Int1

if Int2 > lngMax then lngMax = Int2
if Int3 > lngMax then lngMax = Int3
if Int4 > lngMax then lngMax = Int4
if Int5 > lngMax then lngMax = Int5
 
You can do call out to the worksheet's MAX function...

MaxVal = WorksheetFunction.Max(Int1, Int2, Int3, Int4, Int5)
 
Option Explicit
Dim maxint As String
Dim maxval As Long
Sub CheckMax()
Dim int1 As Long
Dim int2 As Long
Dim int3 As Long
Dim int4 As Long
Dim int5 As Long

int1 = Rnd() * 100
int2 = Rnd() * 100
int3 = Rnd() * 100
int4 = Rnd() * 100
int5 = Rnd() * 100

getmax 1, int1
getmax 2, int2
getmax 3, int2
getmax 4, int4
getmax 5, int5

MsgBox "Integer " & maxint & " hax max value of " & maxval

End Sub
Sub getmax(ByRef text As String, ByRef intval As Long)
If intval > maxval Then
maxval = intval
maxint = text
End If




End Sub
 
Back
Top