Functions

XL Dev Con – Portable Code – Mike Staunton

Dr Mike Staunton author of such modern classics as Advanced Modelling in Finance Using Excel and VBA (The Wiley Finance Series) and Advanced Modelling in Derivatives Using VBA and C++/CLI (The Wiley Finance Series) and Triumph of the Optimists: 101 Years of Global Investment Returns and a few other ones as well, gave a entertaining talk about making code portable.

I just save my code to a USB, and that’s as portable as it get, but that not what Mike was talking about!! ;-) [Hilarious that one Ross!!!]

Mikes suggestion, or if not a direct suggestion then at least the offer of a alternative method, should portability be of interest was to limit the typing conventions, and to avoid where possible the use of function structures or orators  inherent to language type – for example Square Root can be taken as SQRT(A ), or A^int(b) and so on.

Dr Mike Sutton, don't mind your language

After this its a case of editing the file, what Mike referred to as Find and Replace, but what programmers call re-factoring, because their cleaver like that!

The benefit of of all of this was a speed up in the running of the code “for free”, by allowing it to be implemented in a different language, in Mikes case XL DNA, but also as he pointed it at this point it not that far away from C++ if that floats you pointer…

Thanks Mike, who’s next?

How to Use an Excel Formula from VBA

This post is by guest blogger Yoav Eze, his bio can be fond at the bottom of the post. Thanks Yoav!
It’s always nice to get content from other Excel developers, so if you have an interesting story, please feel free to share it here!!! Get in touch: rossmcleanatgoogledotcom
That now makes a list of 2 guest bloggers on MIE in 5 years, Sam and Yoav, I’m hoping to grow that list!!!
Now, over to Yoav.
Excel has a powerful set of formulas, and Visual Basic is a capable language, but have you ever wondered if you can use the Excel formulas within your VBA macros?
There is a way! In fact there are at least two ways to go about this, each option is good for a different scenario.
.
Worksheet Function
If you want to use a standard function within your VBA code, and you don’t need to allow the sheet’s user to change it in anyway (i.e., it’s in the code and shouldn’t be changed), you can use the Excel’s WorksheetFunction object. This object contains all Excel’s functions as member functions, so you can use them directly.
For example:
Application.WorksheetFunction.Sum(TheRange)
This will return the sum of the cells in the range defined by ‘TheRange’.
When a range is required within a worksheetfunction function you will need to use a VBA object.
So this function won’t work:
Application.WorksheetFunction.Sum(A1:A10)
But this will work prefectly:
Application.WorksheetFunction.Sum(Range("A1:A10"))
Additionally, when you type the period after WorksheetFunction in the VBA editor, Excel’s intellisense will display the list of all the functions as they are the members of that object. You can also use the editor’s Object Browser (available via the F2 key or View -> Object Browser) to view those functions, including the arguments they get and the return values types.
.
Evaluate
Here’s where it gets really cool…
Another way to go is to use the Excel’s worksheet or application object’s Evaluate function. This function takes a string and evaluates it, returning either the value it refers to (if it’s a formula) or the range it refers to (if it is a cell or range reference in A1 style).
You can also use square brackets – [] – instead of evaluate.
.
Examples:
Me.Evaluate(“A1″)
will return a reference to cell A1
.
Evaluate("Sum(A1:B3)")
will return the sum of the range A1 to B3
.
[A1].Font.Bold = True
will set font of the A1 cells of the current worksheet to be bold
.
You can combine loops and formulas this way:
For I = 2 To 8
Debug.Print Evaluate("A" & I), Evaluate("countif(A1:A" & (I - 1) & ",A" & I & ")")
Next I
This will print for each cell in range A1:A100 how many times the cell value has appeared before in the range, so for this sheet:
.
..
The output will be:
a1
b0
b1
c0
d0
f0
b2
You can make this code even cooler by using a formula to count how many values there are in column A, like so:
For I = 2 To [counta(A:A)]
Debug.Print Evaluate("A" & I), Evaluate("countif(A1:A" & (I - 1) & ",A" & I & ")")
Next I
Note that I used [counta(A:A)] to count how many cells have values in column A directly from within Visual Basic.
.
Summary
So there you have it! Using those notations, you can combine the flexibility and power of VBA with the ease of use and functionality of Excel Formulas. Can you think of anywhere you could use these features right now?
About the author
Yoav Ezer co-authors the technology and productivity blog Codswallop. He is also the CEO of a company that produces PDF to Excel conversion software.
For more Excel tips from Yoav, join him on Facebook or Twitter

How to use SHOWIF and SHOWUNLESS functions in Excel

This post is by guest blogger Yoav Eze, his bio can be fond at the bottom of the post. Thanks Yoav!
It’s always nice to get content from other Excel developers, so if you have an interesting story, please feel free to share it here!!! Get in touch: rossmcleanatgoogledotcom
That now makes a list of 2 guest bloggers on MIE in 5 years, Sam and Yoav, I’m hoping to grow that list!!!
Now, over to Yoav.

Have you ever in your spreadsheets wanted to show a value only under certain conditions?
We’ve created two functions show-functions (DOWNLAOD THE WORKBOOK HERE)  that will do exactly this. The first accepts a value (which can be a simple value, a cell reference, or a calculation) and a condition as a string (like the MATCH function). It then evaluates the condition on the value. If the condition is true, the value is returned, otherwise an empty string is returned. You can also specify a different return value if the condition is not met by setting a third parameter.

The second function does exactly the same, except that it will return the value if the condition is NOT met, and an empty string (or the third parameter) if the condition IS met.

Here’s an example of how you might use these functions. This is a formula used in one of our spreadsheets:

=IF((SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&B$1,
OFFSET(Payments!$D$2:$D$1000,0,MATCH($A3,Payments!$E$1:$Z$1,0)),"x") -
 SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&$A3,
OFFSET(Payments!$D$2:$D$1000,0,MATCH(B$1,Payments!$E$1:$Z$1,0)),"x"))<=0, "",
SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&B$1,
OFFSET(Payments!$D$2:$D$1000,0,MATCH($A3,Payments!$E$1:$Z$1,0)),"x") -
SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&$A3,
OFFSET(Payments!$D$2:$D$1000,0,MATCH(B$1,Payments!$E$1:$Z$1,0)),"x"))

Looking closely, it’s possible to see that we have the same formula appear twice; once it is checked to see if it’s less then or equal to zero, and again as the result.

Now, the same formula using SHOWIF:

=SHOWIF(SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&B$1,
OFFSET(Payments!$D$2:$D$1000,0,MATCH($A3,Payments!$E$1:$Z$1,0)),"x") -
 SUMIFS(Payments!$D$2:$D$1000,Payments!$C$2:$C$1000,"="&$A3,
OFFSET(Payments!$D$2:$D$1000,0,MATCH(B$1,Payments!$E$1:$Z$1,0)),"x"), "<=0")

That’s MUCH shorter, and probably much easier to understand.

Here are the two functions:

' This function will display the value if it matches the condition;
' otherwise, it will display an empty cell, or
' the third (optional) parameter
Function SHOWIF(value As Variant, condition As String, _
 Optional nonvalue As String = "") As Variant
 If (Not IsError(value) And Application.Evaluate(CStr(value) & condition)) Then
 SHOWIF = value
 Else
 SHOWIF = nonvalue
 End If
End Function
' This function will display the value if it does not matches the condition;
' otherwise, it will display an empty cell, or' the third (optional) parameter
Function SHOWUNLESS(value As Variant, condition As String, _
Optional nonvalue As String = "") As Variant
 If (IsError(value) Or Application.Evaluate(CStr(value) & condition)) Then
 SHOWUNLESS = value
 Else
 SHOWUNLESS = nonvalue
 End If
End Function

To use them, just put in the formula =SHOWIF(value, condition) or =SHOWUNLESS(value, condition)

Grab the file SHOWFunctions.xla which is an Excel Add-in file containing both user-defined functions. It’s packaged as an Excel Add-in so you can easily add it to Excel without having to add the functions to each workbook.

To use it, save it as a file, then open Excel, and add it to the Add-in list. In Excel prior to 2007, you can do this using the Add-in command in the Tools menu (select ‘Browse’ from the dialog that opens and select the saved file to use it). In Excel 2007 and 2010, go to the Add-ins page in the Excel Options dialog, select ‘Excel Add-ins’ in the Manage box and press the Go button. Then use the browse button the same as in the previous versions.
Summary

These functions should make your spreadsheet code much easier to follow and therefore easier to debug. We hope you get a lot of use out of them! Perhaps you could improve or extend our ideas? Please let us know…

About the author

Yoav Ezer co-authors the technology and productivity blog Codswallop. He is also the CEO of a company that produces PDF to XLS conversion software.

For more Excel tips from Yoav, join him on Facebook or Twitter

Introduction to Microsoft Excel Array Functions

This post is by guest blogger Yoav Eze, his bio can be fond at the bottom of the post. Thanks Yoav!
It’s always nice to get content from other Excel developers, so if you have an interesting story, please feel free to share it here!!! Get in touch: rossmcleanatgoogledotcom
That now makes a list of 2 guest bloggers on MIE in 5 years, Sam and Yoav, I’m hoping to grow that list!!!
Now, over to Yoav
.
.
Array functions are a super-useful feature of Microsoft Excel and other competing spreadsheet packages. These functions help you deal with lists, and if you consider how many lists you encounter in your daily life and work, you will see how important making it easy to work with them can be!
To demonstrate how to work with arrays, we downloaded a PDF containing a study of Walmart, and produced a comparison of prices between U.S. and Mexican branches of the stores in Excel. You can take a look at the sample file by downloading Average.xlsx.
As you will see, some of the prices are lower in the U.S., and some are lower in Mexico. How can we see the average ratio of prices that are higher in the U.S., and the average ratio of prices that are lower? We can either break the table into two, one with the lower prices, and the other with higher, or we can use an array function – the conditional average function.
.
Conditional Averages and AverageIf
Up to Excel 2007, the only way to get the result we want was to use array formulas. In Excel 2007, a new formula was introduced to perform just this kind of operation, called AVERAGEIF. We have provided samples of both ways (as array formulas are still supported in Excel 2007).
The AVERAGEIF function takes two parameters (it can take three, but we’ll keep it simple here): the range to work on – just like the regular AVERAGE function – and a condition, represented as a string. The formula evaluates the condition for each cell in the range; if it’s TRUE, then the contents of the cell are counted in the average; if it’s FALSE, they’re ignored. In our sample, the condition is “>100%” for prices that are more expensive in the U.S., and “<100%” for products less expensive.
The older array formulas are a little trickier. To use them, you basically write the regular formula – in this case, AVERAGE; but instead of the range, you put in an IF formula on the range.
The IF formula contains a condition and a value, and they are specified as ranges. This is not valid syntax normally, but it is valid in an array formula. To let Excel know that this is an array formula, you need to press Shift+Ctrl+Enter when you finish typing in the formula instead of the regular Enter. Excel will display curly brackets around the formula in the Formula Bar – note that this is not actually part of the formula, so you cannot just edit those in – you have to do this particular three-key combination enter.
In our sample, we use ‘range>100%’ as the IF condition, and the range (which will be translated as the value of the cell) as the return value if the condition is true. If the condition is false, the IF doesn’t return anything, so the outside AVERAGE formula does not take it into account. To demonstrate this, we can use the ‘Evaluate Formula’ command on the array formula cell:
The formula evaluation starts with the original formula:
We can use the Evaluate button to proceed to the next stage; in this case, expand the range into the values in the cells it contains:
And then, it displays the result of evaluating the condition for each cell:
Now it replaces each TRUE value with the contents of the cells to use in the outside formula; the rest are left as FALSE.
Note that AVERAGE disregards the FALSE values, and so the average comes out correctly.
And finally the result:
Summary
If you ever wanted to calculate, sum, or count items in a list based on certain conditions then you need to get to grips with array formulas. Hopefully this short, hypothetical example has given you a taste for what these useful functions can do and you can now set about working with your own lists.
.
About the author
Yoav Ezer co-authors the technology and productivity blog Codswallop. He is also the CEO of a company that produces PDF to XLS conversion software.
For more Excel tips from Yoav, join him on Facebook or Twitter

The MIE Podcasts – Charles Williams

Charles Williams is probably best known for his work in Excel calculation speed and VBA performance, not to mention the worlds most famous Excel Addin Name Manager. In truth, he’s a expert in many areas of Excel and on top of all this he’s a great bloke. In this podcast we got down and dirty with Excel performance, uncovering some interesting insights along the way. It was a true pleasure talking with Charles.

Charles the First - King of Speed!

Charles in the founder of Decisions Models, a consulting firm specialising in Excel development, and the farther of the FastExcel addin, we talked about this addin and how it works and also his addin loader tool. This is one for the archives!

Thanks to Charles for making the time and sharing his knowledge with us all.