ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Find variable with the highest value (https://www.excelbanter.com/excel-programming/431346-find-variable-highest-value.html)

MJKelly

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

Thanks
Matt

Jim Thomlinson

Find variable with the highest value
 
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
--
HTH...

Jim Thomlinson


"MJKelly" wrote:

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

Thanks
Matt


Rick Rothstein

Find variable with the highest value
 
You can do call out to the worksheet's MAX function...

MaxVal = WorksheetFunction.Max(Int1, Int2, Int3, Int4, Int5)

--
Rick (MVP - Excel)


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

Thanks
Matt



Patrick Molloy

Find variable with the highest value
 
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


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

Thanks
Matt




All times are GMT +1. The time now is 12:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com