ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Return Largest Value in an Array (https://www.excelbanter.com/excel-programming/401137-return-largest-value-array.html)

Rob

Return Largest Value in an Array
 
i have an array ARRAY(10). The array contains different numbers. I would like
to return the largest number in the array to another variable LARGEST.

LARGEST = MAX(ARRAY(10))

Something like =Max(1,2,3,4,5)

Unfortunately I have not been able to determine how to use the MAX math
function to in VBA or if it can be used on an array.

Thank you for your help.

carlo

Return Largest Value in an Array
 
Loop through your array

YourArray = Array(1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1)

For Each arr In YourArray
If largest < arr Then largest = arr
Next arr

hth

Carlo

PS: Maybe there is a shortcut...but at least it works.

On Nov 15, 11:52 am, Rob wrote:
i have an array ARRAY(10). The array contains different numbers. I would like
to return the largest number in the array to another variable LARGEST.

LARGEST = MAX(ARRAY(10))

Something like =Max(1,2,3,4,5)

Unfortunately I have not been able to determine how to use the MAX math
function to in VBA or if it can be used on an array.

Thank you for your help.



Dave Peterson

Return Largest Value in an Array
 
One shortcut would be to use Excel's =max() worksheet function:

Option Explicit
Sub testme()
Dim YourArray As Variant
YourArray = Array(1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1)
MsgBox Application.Max(YourArray)
End Sub



carlo wrote:

Loop through your array

YourArray = Array(1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1)

For Each arr In YourArray
If largest < arr Then largest = arr
Next arr

hth

Carlo

PS: Maybe there is a shortcut...but at least it works.

On Nov 15, 11:52 am, Rob wrote:
i have an array ARRAY(10). The array contains different numbers. I would like
to return the largest number in the array to another variable LARGEST.

LARGEST = MAX(ARRAY(10))

Something like =Max(1,2,3,4,5)

Unfortunately I have not been able to determine how to use the MAX math
function to in VBA or if it can be used on an array.

Thank you for your help.


--

Dave Peterson

Rob

Return Largest Value in an Array
 
Thanks works great!

"carlo" wrote:

Loop through your array

YourArray = Array(1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1)

For Each arr In YourArray
If largest < arr Then largest = arr
Next arr

hth

Carlo

PS: Maybe there is a shortcut...but at least it works.

On Nov 15, 11:52 am, Rob wrote:
i have an array ARRAY(10). The array contains different numbers. I would like
to return the largest number in the array to another variable LARGEST.

LARGEST = MAX(ARRAY(10))

Something like =Max(1,2,3,4,5)

Unfortunately I have not been able to determine how to use the MAX math
function to in VBA or if it can be used on an array.

Thank you for your help.





All times are GMT +1. The time now is 09:09 AM.

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