Article Excel If Statements Look Ups
Article Excel If Statements Look Ups
Article Excel If Statements Look Ups
ThisarticlewillfocusontheappropriateuseofIFstatements,andalsoshowyouwhenit'sbettertomoveintomorerobustformulas likeLOOKUP,VLOOKUP,HLOOKUP&INDEX/MATCH.
NOTES:thisarticleiswrittenforExcel2010anditsRibbonInterface.TheexamplesyouseewerecreatedinExcel2010,butsavedasExcel972003. WorkingexamplesarehostedonMicrosoft's[url=http://cid8cffdec0ce27e813.skydrive.live.com/redir.aspx?resid=8CFFDEC0CE27E813!195]SkyDrive service[/url]foryoutodownload.
IFstatementsareoneofthecoreformulamodelsyoucanuseinExcel,andtheycanbeverypowerfulwithregardstotheirlogic. Verysimplytheyfollowthismethodology:IF(somethingisTrue,thendosomething,otherwisedosomethingelse). =IF(A1="Yes",1,2),whichsimplysaysifA1=Yes,thenreturna1,otherwisereturna2. YoucanalsocombineIFstatementstoevaluatemultiplecriteriabynestingthem,however,manypeopletrytogetIFstatementsto dotoomuch.PreviousversionsofExcelallowedupto7levelsofnestedIFstatements,whichmeansthatyoucouldevaluateupto7 differentcriteria.Excel2007+allows64levelsofnestedIFstatements,butjustbecauseyoucandosomethingdoesn'tmeanyou should! OneofthemostcommonusesofIFstatementsistoreturndatathatmatchesspecificcriteria,orfallswithinarangeofvalues.Here isarelativelycommonexampleofanestedIFstatementtoreturnlettergradesbasedontestscores: =IF(A1>=90,"A",IF(A1>=80,"B",IF(A1>=70,"C",IF(A1>=60,"D",IF(A1<60,"F"))))) Anotheriscalculatingcommissionstatements: =IF(A1>25000,A1*2%,IF(A1>15000,A1*1.5%,IF(A1>5000,A1*1%,0))) Notethatinbothformulas,thecriterianeedtobeorderedsequentiallyinorderfortheformulatocalculatecorrectly.Sointhefirst exampleA1>=90getsevaluatedfirst,andifthatconditionistrue,thentheformulaperformsthecalculationassociatedwiththat condition.Iftheconditionisnttrue,thenitmovesontothesecond,andsoon.Butifyougetyourconditionsoutoforder,thenone conditioncaninvalidatethenextandrenderyourformula(s)useless.OneoftheinherentweaknesseswithIFstatementsisthat theyneedtobepreciseandordered. Whilebothoftheseformulasworkfine,they'reunwieldyandshouldbeavoidedifatallpossible.Why?Primarilybecausethedata intheformulasisstatic,soiftheconditionsdrivingtheformulaeverneedtobechanged,theformulaneedstobemanuallyadjusted asaresult.Granted,thegradesexampleisn'ttoobad,becauseit'snotlikelythatinformationwillchangetoooften,butjustimagine howmuchworkyou'dhavetodoifyouhavealotofformulaslikethecommissionexample,andyouhavetochangethecriteria. Ideally,you'llgetintothehabitofonlyusingIFstatementsforTextcomparisonsliketheearlierexample.Yes/No/Maybeor Male/Femaleevaluationsareverycommon,andthenicethingaboutthemisthatcriteriaarentlikelytochangeveryoften.Ifyou findyourselfwithsituationslikethisthenbyallmeansuseIFstatements,otherwiseit'stimetomoveuptomorerobustalternatives, startingwithLOOKUP. OneoftheprimaryreasonstomoveawayfromIFstatementsformultiplecriteriaissothatyoucanuse"tablebased"reference data.Thisgivesyoutheabilitytohaveyourdatapointsonaworksheet,wherethevaluescanbeeasilychanged,asopposedto hardcodedinaformula,wherechangingthevaluescanbeachallenge.ManyworkbookshavebrokenbecauseofnumerousIF statementsthatsomeonedidntupdate.Inthecommissionexampleabove,whatwouldhappenifyouneededtochangethe2% andyouhadhundredsofformulasdependingonthatone?Itwouldntbefun,andthatsarelativelysmallexample.Imagineone with64conditions!Andthatsifyoucanevenfindtheformulainthefirstplace!Tablebaseddependenciesaremucheasierto changeonthefly,whichcanmeanalotespeciallyifyouredealingwithcomplexmodelsandtestingmultiplecriteria. Note:datatablesthatdrivereferentialformulasdontneedtobehousedintheopenwhereuserscanchangethem.Veryoftenyoullfindsensitive
tableshousedonhiddenworksheets,whereonlythecreator/administratorcanchangethedata.
FollowingareexamplesofaDiscounttable,andaGradestableusingastandardLOOKUPfunction:
Notethefirsttwoformulasusestaticcolumnreferences(2&3),whichtelltheformulatoreturnvaluesfromthesecondandthird columnsrespectively: =VLOOKUP($A8,$A$2:$M$4,2,FALSE) =VLOOKUP($A8,$A$2:$M$4,3,FALSE) Thisisallwellandgood,andformanymodelslikethisoneitsok,butitcanbetimeconsumingtohavetogointoeachformulaand adjustthosecolumnreferences.Thelatterformulas(Marchforward)useatricktomakethecolumnsdynamicbyusingthe COLUMN()functioninstead: =VLOOKUP($A8,$A$2:$M$4,COLUMN(),FALSE) SointhecaseofMarch,itsthe4thcolumntotheright,andCOLUMN()returnsa4,whichfeedsthecorrectnumbertotherestofthe formula.Butthisexampleassumesthatyoullhavearelativelystaticdatasetliketheoneabove.Butwhatifyourcolumnheaders arentalwaysthesame?ThenextexampleshowsyouhowtobuildadynamicVLOOKUPwhereyoudonthavetoknowtheheaders orwheretheyrelocated,yousimplyhavetheformuladoitforyou.
InthiscaseImdoingadynamicHLOOKUPsothatIdonthavetoknowthepositionsoftherowsthatIwanttoreturn.
Andyouwouldneedtomanuallyadjusttherowreferencesasyoucopiedtheformula,soyoudneedtomanuallydeterminewhere GrossProfit,NetProfitandProfit%fellwithinthatrangeandadjusttheformulatosuit. Finally,weremovingontowhatisprobablythemostpowerfuloflookupcombinations,whichiswhenyouuseINDEX&MATCH together.YouvealreadyseenhowyoucanuseMATCHtomakelookupsmoredynamic,butwhathappensifyouneedtodoa lookuptotheleft,whenLOOKUPsonlygototheright?INDEX/MATCHisthesolution,andonceyougetthehangofit,youllbe surprisedhoweasyitis. YouknowthatMATCHreturnsareferenceforavaluefoundinatablebygivingyouthevaluespositioninthetable.INDEXreturns aROW&COLUMNreferenceinarangebasedonwhatrow&columnyoutellittolookin,so: =INDEX(A1:B10,1,2) WouldreturnthevalueincellB1,whereINDEXssyntaxis: =INDEX(Rangeyouwanttolookin,#ofrowstolookdownfromthetopoftherange,#ofcolumnstolooktotherightof therange). Butwait,youmightbethinkingthatscontrarytowhatIsaidaboutINDEX/MATCHbeingabletolookleft!Dontworry,theycanand thefollowingexampleshowshow:
Note:aswithLOOKUPsyoucanreturnamatchingvalueinthesamecolumnasthelookupvalue,butthatsgenerallypointlessunlessyourejusttrying toseeifavalueactuallyexistsinatable.
TheonlyvariationintheformulasisA5:A9,whereImtellingitwhatcolumnheadertofindintheinformationtable. SotheINDEXformulasaysLookintherangeA12:F51andfirstfindtherownumberthatholdstheemployeenameinB12:B51,then findthecolumnnumberthatholdstheheader(EmployeeID#,SSN,Region,etc.)inA11:F11,andreturntheintersectionofthose twovalues.SointhisexampleforSandyStewart,forEmployeeID#,Iwantthe4throwinthefirstcolumn,whichistotheleftofthe namecolumnwerelookingin.Sothatshowyoucanlookuptotheleft. Nowyoumightbethinkingthatsabithardtofathomallatonce,andfranklyitcanbe,buttheresatricktobuildingcomplex formulaslikethis:youdothemonestepatatime.YoullseethedetailofhowIgothereinthesampleworkbook,butIllwalkyou throughitjustincase. First,sinceIknowIllbeusingEmployeenameastheprimarydriverfortherestoftheformula,Istarttherebymatchingasample nameintheNamecolumn.Inthiscase,SandyStewartisrow4,whichIfindwithaMATCHformula: =MATCH($B$4,$B$12:$B$51,0)SofindSandysnameincolumnBandgivemeanexactmatch. ThenstartingwithEmployeeID#,whichIknowiscolumn1,Itakethe4andaddittoanINDEXformula: =INDEX($A$12:$F$51,4,1) WhichgivesmeE033,andthatsthefourthrowinthefirstcolumn. NowIneedtoreplacethe4andthe1.Ialreadyhavethe4,soIcanaddthatnext: =INDEX($A$12:$F$51,MATCH($B$4,$B$12:$B$51,0),1) NextIneedtofindthe1forEmployeeID#,whichIcandowithanotherMATCHformula: =MATCH($A5,$A$11:$F$11,0) Finally,Ireplacethe1withmynewMATCHformula: =INDEX($A$12:$F$51,MATCH($B$4,$B$12:$B$51,0),MATCH($A5,$A$11:$F$11,0)) Nowyoucancopythefinalformuladown,andaslongasyougotyourAbsolutereferencesrightitwillautomaticallyupdateforeach cellitscopiedinto.