Aendrew Rininsland started writing this formula to avoid the common problem of repeated commas when you're concatenating an addresses and empty cells leave you with something like this:
"The House, Bolton Street, , Lancs, , "
His first attempt involved using the IF formula to only concatenate a cell when it contained data. But Excel didn't treat IF in the same way as most programming languages.
So we tried using COUNTA instead (which returns 1 if a cell contains something, and 0 if it contains nothing), and it worked. Here's the formula:
=concatenate(f2,if(counta(g2),concatenate(", ",g2),""),if(counta(h2),concatenate(", ",H2),""))
And broken down:
=concatenate(f2,
Start with the contents of F2
if(counta(g2)
If G2 contains anything
,concatenate(", ",g2)
Then concatenate, inserting a comma, followed by the contents of G2
,"")
But if not, then insert nothing ("").
,if(counta(h2)
If H2 contains anything, again concatenate, inserting a comma, followed by the contents of H2
,concatenate(", ",H2)
But if not, then insert nothing ("").
,""))
Here's an example of how filters can be useful in narrowing down a dataset to relevant companies - as well as avoiding potential mistakes in using =COUNTIF or =SUMIF and counting the same companies twice:
You can see the finished result here.
Here's an Excel formula that's an absolute godsend if you want to know how much money was given to a particular company but don't want to use pivot tables.
=SUMIFS(D:D,B:B,"BIRMINGHAM LEP COMPANY LTD")
This is adding up all values in column D (Invoice amount) where the value in column B is "BIRMINGHAM LEP COMPANY LTD" (The data I used for this is Birmingham Council expenditure for November 2011)
The formula breaks down as follows:
=SUMIFS(
D:D,
The first thing it needs is the range containing values to be added up (i.e. SUMmed) - in this case, column D.
B:B,
Now we tell it what cells contain the values to be tested (IFs) - in this case, column B.
"BIRMINGHAM LEP COMPANY LTD"
And then we tell it what test to apply. In this case, we're saying 'IF the cell contains BIRMINGHAM LEP COMPANY LTD (we add quotation marks to indicate that this is text) then fetch the value from the relevant cell in column D, and add it to all others that meet the same criterion"
You can add further criteria too, with extra commas - for example:
, D:D, ">1000"
Would restrict the calculation further to those cells where the value in the D column was above 1000.
Finally, we end the formula with a closing parenthesis:
)
That's it. As I say, this is something you can also achieve with pivot tables - the main advantage of =SUMIFS (aside from its speed if you know what you're looking for) is that you can use wildcards
Using wildcards
One of the problems with pivot tables is dirty data where the same organisation may be referred to under slightly different names - for example "BBC" or "B.B.C". Wildcards allow you to be widen your scope to include some of these misspellings (although you're better cleaning up the data).
Here are a couple of examples adapting the formula above:
=SUMIFS(D:D,B:B,"BIRMINGHAM L?P COMPANY LTD")
The question mark indicates any single character will do, so this would not only add up values for BIRMINGHAM LEP COMPANY LTD but also BIRMINGHAM LAP COMPANY LTD, BIRMINGHAM LaP COMPANY LTD, and BIRMINGHAM LEP COMPANY LTD with an accent over the E.
The asterisk wildcard does the same, but allows for multiple characters, for example:
=SUMIFS(D:D,B:B,"BIRMINGHAM LEP*COMPANY LTD")
This would again include BIRMINGHAM LEP COMPANY LTD but also BIRMINGHAM LEP COMPANY LTD and BIRMINGHAM LEPCOMPANY LTD - in other words where someone has mistyped the space or failed to type it all (it would also take in BIRMINGHAM LEP HOLIDAY COMPANY LTD or anything else between LEP and COMPANY)
By the way, there's another formula - COUNTIFS - which does a similar thing with counting occurrences. More in a future post...
Although I normally publish posts on spreadsheet formulae here, I've been publishing a series of posts on the =importXML formula on the Online Journalism Blog instead. To make it easier to find them, here are the links: