VBA Functions for Max/Min of a short list of Integers?

Rufus Clupea

Board Regular
Joined
Feb 11, 2019
Messages
85
I'm sure this has come up before (multiple times), and I did search via google (as recommended) but all I could find was functions that would determine Max/Min from a Range of Cells, and that's not what I need. If someone could even point me to a pertinent thread, that'd be great (but please read on :)).

I wrote a couple of simple Functions for returning the Greater of/Lesser of 2 numbers:
Code:
'   RETURNS GREATER OF 2 NUMBERS
Function GreaterOf(x As Long, y As Long)

    If x > y Then GreaterOf = x Else GreaterOf = y
    
End Function 'GreaterOf(x, y)


'   RETURNS LESSER OF 2 NUMBERS
Function LesserOf(x As Long, y As Long)

    If x < y Then LesserOf = x Else LesserOf = y
    
End Function 'LesserOf(x, y)

I realize they're not particularly "elegant" :oops:, but they get the job done (I don't need to know which is greater/less; I just need the value). What I need now is a couple of similar functions that will return the Greater of/Lesser of a short list (up to 5) of Integers, but with more than 2 arguments being optional(?), and not involving/referencing a Worksheet Range (just passing them as arguments).

I spoze something like:

Code:
Function GreatestOf(a As Long, b As Long, c As Long, d As Long, e As Long)

End Function

would work (using 0s for some of the arguments), but I'm at a bit of a loss as to what goes in-between Function and End Function other than a rather large group of If-Thens or a Select Case. Again, I'm thinking this has come up before—I just can't FIND it. :oops:

A single dual-purpose function passing Max or Min as the initial argument would also be fine, e.g:

Code:
Function Max_Min_Of(MaxMin As String, a As Long, b As Long, [c As Long], [d As Long], [e As Long])

End Function

Again, all I need returned is the single Value.

TIA
 
I used to get bad nights dreaming about inputting programs from COMPUTE! It was such a game program (I don't recall the name) that proved my first C64 had a bad sound chip. The random # generator was on the 64's sound chip, and after spending several days typing in the hexadecimal assembler program from COMPUTE!, running the checksums, etc., when I finally started the game, all the spaceships immediately crashed into one of the lower corners. I called Commodore to complain we had gotten a wonky chip, and they seemed skeptical.

"How do you know it's the sound chip?" they asked. I told them the symptoms, and they were surprised I knew enough about their brand-new computer to have DXed that so quickly (it was really obvious). They sent me a new unit--no further questions asked. Had a lot of fun with that machine. Only problem was the power supplies kept burning up. I still have it upstairs in a closet with a box of 5.25 floppies full of games & stuff I wrote.

I won't go into the Atari-Commodore rivalry, the local BBSs, ARPANET, etc.

Shortly after the first IBM PCs came out, I began to lose interest because I just couldn't keep up. My formal education was in a different field, and life took a different path... Now that I'm retired, I'm trying to get back into programming for fun because it was so much fun (and so far still is!) :)
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,216,095
Messages
6,128,790
Members
449,468
Latest member
AGreen17

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