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?
Vasilis DimitrakasAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
result = WorksheetFunction.Min(var1, var2, var3, var4)
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Here is a static way to get the result.  Another option is to have your data in an array or recordset and loop through.

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


varLow = test(varLow, var2)
varLow = test(varLow, var3)
varLow = test(varLow, var4)

response.write "The smallest number is "&varLow

function test(dblA, dblB)

   if dblA < dblB then
        test = dblA
        else
        test = tblB
  end if
     
end function

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
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,
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Vasilis DimitrakasAuthor Commented:
@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.
Martin LissOlder than dirtCommented:
Thanks for the explanation.
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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

Martin LissOlder than dirtCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.