Return the Maximum Value in an Array

michaeldh

Board Regular
Joined
Jun 11, 2002
Messages
201
Hi Guy's,

How can I return the maximum value which is within a VBA array of values. Is there a command that can achieve this or do I have to test each value in the array?

Thanks for your help.

Michael.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
myArray is your array variable name.

MaxVal = Application.WorksheetFunction.Max(myArray)

May be helps.

Suat
 
Upvote 0
Sorry, I often forget explaining "How&Why" part.

Excel worksheet functions can be used in VBA codes with Application.WorksheetFunction property.

We use MAX() worksheet function to find the maximum value in range parameter. Because every range object is also an array object then we can use same function (MAX()) for VBA arrays which we created in our code by giving array name as parameter.

I hope this helps, too.

Suat
 
Upvote 0
Hi,

Be careful when applying the WorksheetFunction.Max to an array with more than 65536 elements.

I don't know where (if) it is explicitly documented, but it appears that the maximum number of elements in any one dimension that can handle the worksheetfunction operators is the number of rows -- the entire column.

Try the following with various numbers...<pre>Sub test()
Dim MyArr, x

ReDim MyArr(1 To 75000)
For x = 1 To 75000
MyArr(x) = x
Next x

MsgBox Application.Max(MyArr)
End Sub</pre>

If you do have a large data set, either redim the array for two or more dimensions or test in a loop.

_________________
Bye,
Jay
This message was edited by Jay Petrulis on 2002-12-27 11:28
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,269
Members
449,075
Latest member
staticfluids

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