Link to home
Start Free TrialLog in
Avatar of Vasilis Dimitrakas
Vasilis DimitrakasFlag for Greece

asked on

How can I find the smallest number between variables?

I have 4 variables which they contain some values.

I have already convert them in numbers with CDbl.

So I have something like that:

var1=CDbl(str1)
var2=CDbl(str2)
var3=CDbl(str3)
var4=CDbl(str4)

Open in new window


How can I find the smallest number between var1, var2, var3 and var4?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

result = WorksheetFunction.Min(var1, var2, var3, var4)
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Vasilis, I'm not complaining about you choosing Scott's answer, you win some and you lose some, but I'm just wondering why you chose it since my one-line code does what you asked for,
Avatar of Vasilis Dimitrakas

ASKER

@Martin Liss
Dear Martin,
Thank you for you interest in my question.
Obviously, I don’t have any reason choosing the other answer. The only reason is that I am not familiar at all with the “WorksheetFunction.Min” and with the first look it seams that is for Excel sums. The other solution, was more familiar to me for using it in my classic asp web page.
Thank you once again.
Thanks for the explanation.
Martin,

Your solution is for Excel-vba. This is for vbscript which is not the same thing. However, there is a similar function called LBound which will return the lowest item in an array just as Ubound will return the highest.

If you have your data in an array instead of the way it is presented, you could use Lbound.

var1=CDbl(str1)
var2=CDbl(str2)
var3=CDbl(str3)
var4=CDbl(str4)

MyArray = (var1,var2,var3,var4)
SmallestNumber = LBound(MyArray)

Open in new window


https://www.w3schools.com/asp/func_lbound.asp
https://www.w3schools.com/asp/func_array.asp

Arrays are not always easy to work with in vbscript/asp but in this case, getting the smallest number in an array using LBound could be a good choice and would be similar to Excel VBA
result = WorksheetFunction.Min(var1, var2, var3, var4)

Open in new window

Thanks Scott. Im sure your aware that LBound is also available in VBA, but in VBA SmallestNumber = LBound(MyArray) would just return the Index of the lower bound of the MyArray.