Does VBA have Native Functions for MIN, MAX

Jeffrey Mahoney

Well-known Member
Joined
May 31, 2015
Messages
3,241
Office Version
  1. 365
Platform
  1. Windows
I'm dabbling more into the VBA code for Outlook recently. I have external systems that send me emails with updates for services and notifications. The format of these emails are atrocious.

What I've noticed is that VBA doesn't have a built MIN and MAX function. Since I work primarily in Excel, I'm used to using Application.Min(). Outlook doesn't have that.

Does anybody find it strange that such a simple function doesn't exist? Am I looking at this wrong? Since early days of BASIC those were available. Comparing two variables with IF statements isn't too bad, but start adding 3 or more variables and now the IF structure gets kinda difficult.
 
Yeah, I guess I wasn't reading very closely.

Of course, if you're working in Outlook, and you have set a reference to Excel, you could use Excel.Min and Excel.Max.
 
Upvote 0
I can't use WorksheetFunction in Outlook or Word VBA.

@Rick. Nice! Now, of course I had to pick it apart. I ran that through VBA and through a formula in a cell also. Another strange thing. The TRUE value in an Excel formula equates to a 1, whereas the value for TRUE in VBA equates to -1. Gotta love those MS folks and the inconsistency!

The first half of the formula: -A * (A >= B) equates to zero if False and the second half is the same B * (B > A) .

I'm most likely to use the solution that IRobbo provided because I can feed it multiple values if needed and keep it in a library of functions. Thank you all.
 
Upvote 0
whereas the value for TRUE in VBA equates to -1. Gotta love those MS folks and the inconsistency!
It is because VBA uses the Binary representation of True as 1111111111 and this equates to a value of -1.
To see this in a spreadsheet...
Book1
A
11111111111
2-1
Sheet2
Cell Formulas
RangeFormula
A1A1=DEC2BIN(-1)
A2A2=BIN2DEC(1111111111)
 
Upvote 0
@Jeffrey Mahoney here's a little cleaner version of the function.

VBA Code:
Function iMax(ParamArray Vals() As Variant)
For Each v In Vals
    If v > iMax Then iMax = v
Next v
End Function
 
Upvote 0

Forum statistics

Threads
1,226,835
Messages
6,193,241
Members
453,783
Latest member
Chandni

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