# Thread: Smallest of 3 numbers

1. ## Smallest of 3 numbers

What is the easiest and simplest way to get the smallest value of three numbers. There is the possibility of two or three values the same

2. ## Re: Smallest of 3 numbers

May want to test this well, off top of my head...
Code:
```If Nr1 =< Nr2 And Nr1 =<  Nr3 Then
' nr1 is smallest; though numbers 2 & 3 may be equal or greater
ElseIf Nr2 =<  Nr3 Then
' nr2 is smallest; though Nr3 may be equal or greater, but Nr1 is greater else triggered above
Else
' nr3 is smallest by elimination
End If```
edited: you can remove the = from the comparisons above if first-in doesn't matter when a tie exists. In other words, comparing 1,2,1 will return the 3rd number as the smallest instead of the 1st number.

3. ## Re: Smallest of 3 numbers

I would say depending on the need, creating a function to use would be easiest in the long run, and also simplest to use once written. You could use variant parameter array to the function to handle 2 to n comparisons, i.e. z = Min(a, b) or z = Min(a, b, c, d), ...

If you just want to compare three values you should need two If tests as a minimum, and I would use an extra variable to store the result, to keep it to two.
You could use IIf and put the two IIf statements in one line, to appear simplest
Code:
`d = IIf(a < b, a, b): d = IIf(d < c, d, c)`

p.s. As mentioned in the first part of my post, but limited to comparing two values, a Min function could be written to be reused.
Code:
```Private Function Min(a As Variant, b As Variant) As Variant
If a < b Then
Min = a
Else
Min = b
End If
End Function```
And then you could set d to the minimum value like this:
Code:
`d = Min(Min(a,b), c)`

4. ## Re: Smallest of 3 numbers

IIRC we had a thread exactly like this one a few years back.

EDIT: Found it