• Function: IF, AND & OR

    Want to know how to make your spreadsheet look like it’s a thinking being?  Looking for a way to make your data talk back to you?

    The most interesting aspect about Excel is the ability to execute logical equations or FUNCTIONS.

    IF

    One of my favourite functions is the IF function. Remember that this is a formula and as such, whenever you type it into a cell, you have to begin with a ‘=’ sign.

    The structure of the IF function has 3 parts and looks like this:

    =if(1. a logical or mathematical expression; usually referring to another cell, like if cell A1 is equal to 3 or if cell A1 is equal to cell B1, 2.then what you would like this cell to say or do if the expression indicated in 1. is true, 3. Lastly, what you would like this cell to say or do if the expression in 1. is false)

    Here’s an example:

    =if(A1=3,1,0)

    IF 1  IF 3

    IF 2

    What I have done is create an IF function that checks if cell A1 is equal to 3. If it is equal to 3, the cell the formula is in then gives you a value of 1.  If cell A1 is any other value besides 3, the cell then gives you a value of 0.  So, what right?

    Like so many things in Excel, the IF function is so, so much more!

    Here’s another example:

    =IF(A1=3,”I do not want a 3!”,”Not a 3 is fine.”)

    Notice that the text in the IF function is framed by “ ”. All text in any function – if you want it to behave like text, must be framed by “ ”.  What we have done is create a function that checks whether cell A1 is equal to 3.  If cell A1 is equal to 3, the cell then gives you the text, “I do not want a 3!”.

    IF 5

    If cell A1 is any other value except 3, the cell then gives you the text, ”Not a 3 is fine.”

    IF 4

    This makes for wonderfully interactive formulas that not only do the work, but add personality!

    Again, so what? Really?

    Here’s another one. Let’s say you have 1,000 random numbers ranging from 0 to 9999 and you wanted to know which ones were between 13 and 54.

    Here’s how we’d do it:

    =IF(AND(A1<=54,A1>=13),”Between 13 and 54!”,”Out of range.”)

    IF 6

    Whoa, what the hell was that? The IF function allows a combination with 2 other types of functions, AND and OR.  We’ll talk about that in a moment.

    So the formula above says that if cell A1 is less than or equal 54, indicated by the <= AND if cell A1 is ALSO more than or equal to 13, indicated by the >=, then the cell tells you, “Between 13 and 54!”. If cell A1 is outside of this magical 13 -54 range, the cell then says, “Out of range.”

    The best part is that by placing your mouse pointer at the LOWER RIGHT corner of a cell, then holding the left-button, you can move the mouse pointer downwards, thereby COPYING the formula onto the cells below. Dragging upwards copies the cell contents upwards too.  You will notice that in the cell along the same row as A9, the formula does not mention cell A1 anymore, but A9.

    IF 7

    Neat, huh?

    AND

    The AND function is used in combination with the IF function to add more logical expressions to the first one.

    So, the example:

    =IF(AND(A1<=54,A1>=13), “Between 13 and 54!”,”Out of range.”)

    Means that BOTH the conditions A1<=54 and A1>=13 MUST be fulfilled before the text “Between 13 and 54!” appears. Otherwise, “Out of range.” appears instead.

    OR

    The OR function is similar to the AND function, but as you probably guess, you sly fox you, when used in combination with the IF function, assesses whether EITHER of the logical expressions is fulfilled before returning the values if the expressions are true.

    Here’s an example:

    =IF(OR(A2=1,B2=1),”Team 1 in Season 1 or 2″,”Team 1 absent”)

    IF OR 1

    Let’s take the columns A and B as a team roster for Seasons 1 and 2. The expression using IF and OR above checks whether Team 1 is in EITHER Season 1 or 2, giving you the text, “Team 1 in Season 1 or 2″.  If Team 1 is absent from both seasons, the text, “Team 1 absent” presents itself.

    Now, note the difference if we had used IF and AND instead:

    =IF(AND(A2=1,B2=1),”Team 1 in Season 1 or 2″,”Team 1 absent”)

    IF AND 1

    Remember that the IF and AND function when used together, checks if BOTH expressions are true before returning the text, “Team 1 in Season 1 or 2″. Since Team 1 is only in either Season 1 or 2 but never both at the same time, “Team 1 absent” is returned.  This happens because, yes, BOTH conditions, that Team 1 would be in Season 1 and 2 at the same time, were not met.

    I hope this gives you a good illustration of how the AND and OR functions work and their differences. Did I mention that you can use IF, AND and OR at the same time?  Okay, okay, another time.

    Here’s something extra:

    Let’s say you have 9 teams, named 1 through 9 and you want to group teams 1 to 3 into a separate team, called Team Badger, teams 4 to 6 into another team, called Team Skunk and teams 5 to 9 into a final team called Team Dead. How would we do that? Here’s how:

    =IF(OR(A2=1,A2=2,A2=3),”Team Badger”,IF(OR(A2=4,A2=5,A2=6),”Team Skunk”,IF(OR(A2=7,A2=8,A2=9),”Team Dead”,”Not a Team”)))

    IF adv 1

    So, not only are we able to identify the teams to be grouped into new teams when they aren’t in order, we are also able to tell when there are values not in our desired line up.

    Here’s something extra:

    Often, you may want to make use of the logical aspects of the IF function to do interesting things.

    When working with large amounts of data, you may encounter a situation like this where you need to copy numbers multiple times in order to fill up gaps. It is a REAL PAIN.

    So, using the IF function, we would be able to fix that. Here’s how:

    =IF(A2=0,B1,A2)

    IF Logic1

    Voodoo!!!

     

  • Number Formatting

    For those of you dealing with large sums of numbers – maybe in a currency that has a lot of zeros in them, here’s something that may help!

    Step 1:

    Let’s start with some data.  What if we needed to format the numbers in a way that multiplies every number by 1,000?

    Numbers

    Step 2:

    Let’s select the entire column.

    Step 3:

    Now, right click and you will see a series of options.  Click on ‘Format Cells’.

    Numbers2

    Step 4:

    Now, click on the ‘Number’ tab and select ‘Custom’.

    Numbers3

    Step 5:

    Now, enter the following into the box:

    Numbers6

    Numbers4

    Numbers5

    Now, all the numbers have been automatically multiplied by 1,000!

    Neat!

  • Welcome to ExcelPunks!

    Ever since Uncle Bill dreamed of putting a computer in every home, his company’s programs have become as much a part of our lives as well…computers.

    Excel isn’t new to any of us, but haven’t you wondered if it could be used as more than just a piece of digital paper?

    Ever wondered if Excel could start working FOR you?  Making YOUR life easier?  Allowing you to spend more time developing your staff or talking to your kids, or maybe spending an extra hour with a loved one who doesn’t have much time?

    I know, we don’t normally think about things like that, but that’s the truth of how some of us see work.  We place work above our own lives and before we know it, life is over.  There must be a better way, shouldn’t there?

    Someone taught ME once and I’d like to pay that forward in my own little way.

    By teaching others how to use Excel to add VALUE to their lives.  To create solutions that get you home on time.  To provide that little shortcut that gives you that extra day to spend with that special someone.

    Yes, if you absorb all there is in this website, you will own Excel like a boss.  And you will start being the boss of your own life.

    Don’t let life pass you by – for all the thoughts we have not thought, for all that should have been said that was not said and for all that should have been done, that was not done – let’s make a great account for all the days of this thing called life.

    Please feel free to leave a message if there’s something you’d like to know – I’ll do my best to include it in the next post, or point you in the right direction.

    Enjoy your stay!