# Thread: VBS Math function "MAX"

1. ## VBS Math function "MAX"

Hi all,

i need to find max of 3 numeric values , i need to know is there any inbuilt function for it.

i did search abt it i found math.max can be used in Jscript, is any function available for Vbscript.

thanx

2. ## Re: VBS Math function "MAX"

here is the function: input paramenter is am array of numbers. Enjoy.

</font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">
Function Max(ArrayOfNumbers)
Dim i
Dim CurrentMax
CurrentMax = Null

For i = LBound(ArrayOfNumbers) to UBound(ArrayOfNumbers)
If IsNumeric(ArrayOfNumbers(I)) Then
If CDbl(ArrayOfNumbers(I)) &gt; CurrentMax Or IsNull(CurrentMax) Then
CurrentMax = CDbl(ArrayOfNumbers(I))
End If
End If
Next

Max = CurrentMax
End Function
</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">

[ 03-29-2006, 08:03 AM: Message edited by: green_go ]

3. ## Re: VBS Math function "MAX"

UBound only gives you the size of the array (how many elements). Refer to this: http://www.w3schools.com/vbscript/func_ubound.asp, as you see in its example you have an array of 10 elements but only 5 used. It does not give you the element with the maximum value, it gives you the size of the array.

If you need the 'value' that is the Maximum you will need to load the array and then do a sort in ascending order, then you go and get the last element of the array (Ubound will work for this). Unfortunately VBScript is not very statistical friendly. You will need to write your own functions to do this.

So... your assignment is to learn how to do a bubble sort (ascending order) in VBScript, then pull the last value in the array which will give you the Max.

Jim

4. ## Re: VBS Math function "MAX"

jimhazen: yes, I've hurried up. Maybe it's because of the sun eclipse [img]images/icons/smile.gif[/img] I've corrected my post.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.