Sunteți pe pagina 1din 8

UsercommunitysupportforumforApacheOpenOffice,LibreOfficeandalltheOpenOffice.

orgderivatives

Skiptocontent

Search Search
Advancedsearch

BoardindexGettingstartedTutorialsCalc
Changefontsize
Printview

FAQ
Register
Login

[Tutorial]VLOOKUPquestionsandanswers
Forumrules
Noquestioninthisforumplease
Foranyquestionrelatedtoatopic,createanewthreadintherelevantsection.
Postareply
Searchthistopic Search

3postsPage1of1

[Tutorial]VLOOKUPquestionsandanswers
byMrProgrammerMonJan09,20129:20pm

Thereareseveral"lookup"functionsinCalc:VLOOKUP,HLOOKUP,LOOKUP,andMATCH.Thistutorial
isforVLOOKUPbuttheothersareeasytolearnonceyouunderstandhowVLOOKUPworks.VLOOKUP
searchesforavalueintheleftcolumnofatableandreturnsthevalueinthesamerowofanother
columnofthattable.

Thesyntaxis=VLOOKUP(valueToSearchFortableToSearchIncolumnIdentifierisTableSorted?).

valueToSearchForThisisthevaluewhichVLOOKUPuseswhensearchingtableToSearchIn.Normallyit
isareferencetoacell,butitcouldbeaconstantoranexpression.Thatis,normallypeopleuse
=VLOOKUP(D5)butyoucoulduse=VLOOKUP(3.14159)or=VLOOKUP(355/113
).

tableToSearchInThisisrangeofoneormorerowsandoneormorecolumns.VLOOKUPsearchesforthe
valueToSearchForinthefirstcolumnoftableToSearchIn.NormallyVLOOKUP'ssecondparameterisa
specificreferencetoarangeofcells,like$F$2:$H$14.Youalmostcertainlywanttouseanabsoluterange
($F$2:$H$14)ratherthanarelativerange(F2:H14)sothattherangeisnotadjustediftheVLOOKUP
formulaiscopiedtoothercells.Readsection8(Usingformulasandcellreferences)inTenconceptsthat
everyCalcusershouldknowifyoudon'tunderstandthedifferencebetweenabsoluteandrelativereferences.
columnIdentifierThisidentifiesthecolumnusedforthefunctionresultafterthesearchhaslocatedavalue
inthefirstcolumnoftableToSearchIn.ForVLOOKUP,thecolumnsoftableToSearchInareidentifiedas1,
2,3,.Sofortherange$F$2:$H$14,columnIdentifier1isF,columnIdentifier2isG,andcolumnIdentifier
3isH.=VLOOKUP($F$2:$H$142)meanstoperformthesearchincolumnFandreturnthevaluein
thesamerowofcolumnG.AcolumnIndentifierof3wouldmeantoreturnthevalueinthesamerowof
columnH.

isTableSorted?Basedonthequestionsaskedinthisforum,almosteveryoneshouldsupply0asthefourth
parameterofVLOOKUP,whichmeansthatyouwantanexactmatchforvalueToSearchFor.Since0isnot
thedefault,besurenottoomitit.Therearequestionsbelowthataddresswhatwouldhappenifyoudid.

Example:C2containsformula=VLOOKUP(B2$F$2:$H$1430).Thismeans:lookforB+incolumnFand
returnthecorrespondingvaluefromcolumnH,whichis3.3.Notethatthefourthparameteris0.

VLOOKUP.ods
(8.85KiB)Downloaded5104times

Veryimportant:Twooftheoptions(OpenOffice.org>PreferencesonaMac,Tools>Optionsonother
platforms)affectseveralfunctions,includingVLOOKUP.BothoftheseoptionsareintheCalc>Calculate
section:

Searchcriteria=and<>mustapplytowholecellsIfyouuncheckthisoptiontextsearchesin
VLOOKUPcanmatchasubstringofthevaluesinthetablesointheexampleasearchforBwillfind
B+.Youalmostcertainlywanttoenablethisoptionsothatanexactmatchmustoccur.Enablingthe
optionalsomakesyourVLOOKUPformulascompatiblewithExcel.
EnableregularexpressionsinformulasUnlessyouunderstandwhat"regularexpressions"are(see
Help)andunlessyouspecificallywanttousetheminyourspreadsheet,youwillwanttouncheck
EnableregularexpressionsinformulasbecausethisoptioncanmakeVLOOKUPdifficulttouse.
UncheckingtheoptionalsomakesyourVLOOKUPformulascompatiblewithExcel.Thequestions
belowaddresswhathappensifyouenablethisoption.
Summary:CheckSearchwholecellsanduncheckRegularexpressions

Q1.WhathappensifVLOOKUPcan'tmatchthevalueToSearchFor?
A1.Itreturnstheerror"Valuenotavailable"(#N/A).

Q2.Idon'twanttosee#N/A.HowcanIgetVLOOKUPtoleavethecellemptyifitcan'tfind
valueToSearchFor?
A2.Use=IF(ISNA(VLOOKUP())""VLOOKUP()).Orput=VLOOKUP(
)inanoffscreencolumn(sayZA2)anduse=IF(ISNA(ZA2)""ZA2)inthemainpartofyourspreadsheet.
Thismeans:

IfVLOOKUPreturned#N/A,thecell'svalueistheemptystring("")otherwisethecell'svalueisthe
VLOOKUPresult.
Q3.HowcanIprovidea"default"valueintableToSearchIn,thatis,avaluethatwillbereturnedif
valueToSearchForisn'tfound?
A3.Youcan'tputadefaultinthetable,butyoucanputadefaultintheformula.Use
=IF(ISNA(VLOOKUP())defaultValueVLOOKUP())or,usingthetechniqueabove,
=IF(ISNA(ZA2)defaultValueZA2).

Q4.CanIuseVLOOKUPtotellifmeifvalueToSearchForisfoundinalist?Idon'tneedtodoatable
lookupIjustwantayesornoanswer.
A4.Yes,use=NOT(ISNA(VLOOKUP(valueToSearchForyourList10))).TRUEmeansthesearchwas
successful.

Q5.Ican'tgetyourexampletowork.Itfinds4.3insteadof4.0forthelookupofA.Whydoesn'titworkfor
me?
A5.Youprobablydidn'tenabletheSearchcriteria=and<>mustapplytowholecellsoptionsoVLOOKUP
foundA+inthetablewhensearchingforA.

Q6.Ican'tgetyourexampletowork.Itfinds3.0insteadof3.3forthelookupofB+.Whydoesn'titworkfor
me?
A6.Youprobablydidn'tunchecktheEnableregularexpressionsinformulasoption.

Q7.OK,uncheckingoptionEnableregularexpressionsinformulasallowsVLOOKUPtofind3.3forB+.
WhydoesVLOOKUPgivethewrongansweriftheoptionischecked?
A7.VLOOKUPisreturningthecorrectanswerinbothsituations.It'sjustthat"regularexpression"matching
worksdifferently.Itisbeyondthescopeofthistutorialtoexplainregularexpressions,buttheyareawayof
performingpatternmatching.InthisparticularcasethepatternB+matchesanynumberofB'slikeBorBB
orBBB.Inapattern,theplussignmeans"atleastone"anddoesnotmean"+".SopatternB+matchesBin
thetableandthevalueis3.0.PatternB+doesnotmatchB+inthetable.

Q8.ArethereotherpatterncharacterswhatIneedtobecarefulabout?
A8.Yes:anyof.^$*+?\|[](){}Becausetherearesomanyofthem,it'sbesttouncheckEnableregular
expressionsinformulasbecausethentheywon'tcausetroubleforyou.Seeotherexamplesofpattern
matchingdifficultiesinLookuptableforgradecalculation.

Q9.IhaveatablethatIwanttousewithVLOOKUPbutthecolumnIwanttosearchisn'ttheleftmostone.
WhatcanIdo?
A9.ThecolumnIdentifiermustbeapositivenumbersoit'seasiestifyourestructureyourtablesoitmeets
VLOOKUP'srequirement.Ifyoudon'twanttodothat,youcanuseMATCHandINDEXasshowninMatch
Formula.

Q10.IseeIcanuseaconstantforthefirstparameterofVLOOKUP.CanIuseaconstantforthetable(the
secondparameter)too?
A10.Yes,butyouwillneedtouseanarrayconstant.SeeA20(below)foranexample.

Q11.Ican'tgetyourexampletowork.Itcorrrectlyfinds3.3forthelookupofB+,but3.7forthelookupof
B.IhaveuncheckedEnableregularexpressionsinformulas.Whydoesn'titworkforme?
A11.Ensurethatyousupplied0asthefourthparameterforVLOOKUP.Ifyouuseanonzerovalue,orif
youomitthefourthparameter,you'retellingVLOOKUPthattableToSearchInissortedinascendingorder.
Thetableinthetutorial'sexampleisnotsortedsoVLOOKUPmightreturnunpredictablevalues.

Q12.DoesVLOOKUPworkdifferentlyiftableToSearchInissortedinascendingorderandIuse
=VLOOKUP(1)or=VLOOKUP()?
A12.Yes!IfyoutoldVLOOKUPthatthetableissorted,thefunctionsearchesthefirstcolumnforthe
largestvaluewhichdoesnotexceedvalueToSearchFor.Anexactmatchisnotrequired.Soasearchfor
3.14159inthevalues1,2,3,4,5willfind3andVLOOKUPwillreturnthecorrespondingvaluefrom
columnIdentifier.IfyouwantVLOOKUPtofindonlyexactmatches,youmustuse=VLOOKUP(
0).

Q13.I'musingthe"sorted"VLOOKUPsearch,thatis=VLOOKUP(1)or=VLOOKUP(),
andamgettingunexpectedresults.Why?
A13.IfyoutoldVLOOKUPthattableToSearchInissorted,itisimperativethatthisisreallythecase.Inthis
situation,VLOOKUPisallowedtousebinarysearchtolocatevalueToSearchForandCalcdoesnot
necesssarilybeginwiththefirstrowoftableToSearchInandcheckeachsuccessivevalue.Therefore,ifthe
tableisn'tsortedyoucannoteasilypredictwhichrowwillbelocated.Eitherusetheexactmatchsearch,
=VLOOKUP(0),oruseData>SortontableToSearchIntoputitinascendingorder.

Q14.MytableToSearchInhasnumbersinthefirstcolumn.WhenIuse=VLOOKUP(0)itreturns
#N/Aeventhoughthenumberisinthetable.IhaveuncheckedtheEnableregularexpressionsinformulas
option.What'swrong?
A14.AnonemptycellinCalccanholdeitheranumberortextasexplainedinTenconceptsthateveryCalc
usershouldknow.Numbersandtextaredifferentthenumber12isnotthesameasthetwocharactertext
"12".Sincethey'redifferent,asearchforanumericvalueToSearchForwillnotmatchtextvaluesinthefirst
columnoftableToSearchIn.Ifyoususpectthisistheproblem,useView>ValueHighlightingsoyoucantell
ifthevalueisnumeric(blue)ortext(black).Numeric(blue)valueToSearchFordoesnotmatchtext(black)
intableToSearchIn.Insomecasesatext(black)valueToSearchFormightmatchnumbers(blue)in
tableToSearchIn,butit'sriskytorelyonthat.Eitheruseallnumbersoralltextforyoursearches.

Q15.OK,that'stheproblem(numbersdon'tmatchtext).HowcanIfixthat?
A15.SelectthefirstcolumnoftableToSearchInandusetheData>TexttoColumnsdialog.Itcanconvert
numberstotextortexttonumbers.

Q16.The"numbersdon'tmatchtext"difficultyisn'tmyproblem.BothvalueToSearchForandthevaluesin
thefirstcolumnoftableToSearchInarenumbers.Whatelsecouldbewrong?
A16.VLOOKUPsearchesbasedonthevaluesinthecells,notthevaluesthatyouseeonthescreen.Those
mightbedifferent.ForexampleCalc'sdefaultistodisplayonlytwodecimalplacesinacell.Ifthecell's
valueis2.99999967(perhapstheresultofaformula)itwilldisplayas3.Asearchforthecell'svalueinthe
table1,2,3,4,5willfailbecause2.99999967isn'tfoundeventhoughitlooksasifVLOOKUPissearching
forthe3thatyouseeonthescreen.

Q17.MytableToSearchInhastextinthefirstcolumn.WhenIuse=VLOOKUP(0)itreturns#N/A
eventhoughthattextisinthetable.IhaveuncheckedtheEnableregularexpressionsinformulasoption.
What'swrong?
A17.ThematchingdonebyVLOOKUPallowslowercasetomatchuppercaseandviceversabutdoesnot
ignoreleadingortrailingspaces.Sousingtutorial'sexample,asearchfor"A"(theletterAfollowedbya
space)willfail.Itcanbedifficulttospotproblemslikethatbecauseacellcontaining"A"looksjustlikeone
containing"A".Checkthelengthsofthevaluesinyourcells,forexample=LEN(B4)and=LEN(F3)should
bothbe1.YoucoulduseEdit>Find&Replacetoremoveunwantedleadingortrailingspaces.See
VLOOKUPRetrievesDatafromIncorrectRow.

Q18.Ihaveaspreadsheetwheresomeoneused1forcolumnIdentifier,thatis,theformulais
=VLOOKUP(valueToSearchFor1).Isn'tthatthesameasformula=valueToSearchFor,aslongasthe
valueisfoundinthetable?
A18.No,becausethefourthparameterwasomittedanditisn'tan"exactmatch"searchsearchingfor
3.14159in1,2,3,4,5willreturn3insteadof3.14159.

Q19.ThefirstfewVLOOKUPsworkbutlaterrowsfail.Anyideas?
A19.Yes,checktobesurethatyouusedanabsolutereference($$)forthesecondparameterasshown
inthetutorial'sexample.

Q20.Ineedtocalculatethepayratebasedonhoursworked:<40$194045$19.504550$2050
55$20.5055+$21.IamusingIFfunctions
=IF(A22<4019IF(A22<4519.5IF(A22<5020IF(A22<5520.521))))butnowIhavetoaddanotherrate
andtheformulaisgettingtoocomplicated.HowdoIuseVLOOKUPtosimplifythis?
A20.Useanarrayconstantforthesecondparameter=VLOOKUP(A22
{019|4019.5|4520|5020.5|5521}21)insteadofnestedIFstatements.Thenyoucanjustadjustthearray
constantinsteadofnestinganotherIFfunction.SeeHelpforarrayconstantsyntax.
Q21.IneedtodifferentiatebetweenupperandlowercaseintableToSearchIn.VLOOKUPisignoringthe
caseandfindingvaluesIdon'twant.HowdoIavoidthatproblem?
A21.YoucannotuseVLOOKUP(orLOOKUPorHLOOKUPorMATCH)ifyouneedtodifferentiate
betweenupperandlowercase.Itisbeyondthescopeofthistutorialtodiscusswaystosolvethatproblem.

Q22.IneedtosearchatablebytwomatchingvalueToSearchForkeys.Thatis,Iwantthefirstkeytocheck
columnAandthesecondkeytocheckcolumnB,thenreturnthetableentrywhichexactlymatchesbothof
them.HowdoIdothat?
A22.IfyouwanttouseVLOOKUP,youwillhavetocreateathirdcolumnCwiththeconcatenationofthe
twokeysinAandBanduseVLOOKUPtosearchfortheconcatenationofthetwokeysincolumnC.See
VLOOKUPacrossmultiplecells.AnotherapproachisdescribedinSearchingbyrows&columns.

NotethatquestionsarenotallowedintheTutorialssectionoftheforum.AskthemintheApplications
>Calcsection.IfyourVLOOKUPfunctiondoesn'tworkthewayyouexpect,considerthattherearemany
possibledifficulties(especiallyQ5,Q6,Q11,Q13,Q14,Q16,Q17,andQ19).Ifyou'retriedthesolutions
hereandarestillstumped,whencreatinganewtopicintheCalcsectionbesuretoattachyourdocument
(notapictureofit).Otherwiseitishardforthevolunteerstohelpyoubecauseinmostcasesapicturedoesn't
haveenoughinformationtodiagnosetheproblem.
LasteditedbyMrProgrammeronSunMar30,201410:58pm,edited2timesintotal.
Mr.Programmer
OpenOffice3.2.0Build9483onMacOSX10.9.5.ThelocaleforanymenusorCalcformulasinmyposts
isEnglish(USA).

MrProgrammer
Volunteer

Posts:2821
Joined:FriJun04,20107:57pm
Location:Wisconsin,USA

Top

Re:[Tutorial]VLOOKUPquestionsandanswers
byVilleroyThuMar22,20121:07am

KnowingeverythingaboutVLOOKUPinunordered"databasemode",theordered"defaultmode"remains
unclear.
=VLOOKUP(valueToSearchFortableToSearchIncolumnIdentifier0)
=VLOOKUP(valueToSearchFortableToSearchIncolumnIdentifier1)
Ifthelastargumentismissing,adefaultmode1isassumed.Thetwomodesserveverydifferentpurposes.
TheunorderedmodereturnsavaluefromthefirstpositionwherevalueToSearchFormatchesexactlyavalue
inthefirstcolumnoftableToSearchIn.Thismodefetchessomevalueofonedistinctlistitem.Itfailswith
errorvalue#N/A(notavaillable)ifthereisnomatchingitem.
TheorderedmodereturnsavaluefromthelastpositionwherevalueToSearchForissmallerthanorequaltoa
valueinthefirstcolumnoftableToSearchIn.Thismodefetchesaclosestmatchthatfallsintosomerangeof
valuesonanorderedscale.Itfailswitherrorvalue#N/Aifthesearchvalueissmallerthanthesmallestlist
item.
Theattachedfilehas3sheets.Thefirstonedemonstratesthedefaultmodeforalllookupfunctions
LOOKUP,VLOOKUPandMATCH.LOOKUPsupportsorderedmodeonly.
Thesecondsheetdemonstratesa3rddescendingorderedmodethatcanbeusedwithMATCH.
Thethirdsheettriestoexplainwhyorderedmoderequiressortedlists.Itdemonstrateswhathappenswhen
thelistisnotproperlysorted.

Attachments

ordered_scale_lookup.ods
Demonstratingorderedscalelookup
(27.8KiB)Downloaded885times

LasteditedbyVilleroyonSatDec08,20129:24pm,edited1timeintotal.
Please,editthistopic'sinitialpostandadd"[Solved]"tothesubjectlineifyourproblemhasbeensolved.
Ubuntu16.04,OpenOffice4.x&LibreOffice5.x

Villeroy
Volunteer

Posts:23375
Joined:MonOct08,20071:35am
Location:Germany

Top

Re:[Tutorial]VLOOKUPquestionsandanswers
byMrProgrammerSunJul22,20123:15am

Q23.WhathappensifIuse=VLOOKUP()or=VLOOKUP(1)butthetableToSearchIn
isn'tinascendingorder?
A23:AsexplainedinA13above,VLOOKUPwouldbeallowedtouseabinarysearchtolocate
valueToSearchForsothesimpleanswer,fornonprogrammers,isthatyouwillgetunpredictableresultsand
yourspreadsheetwillproduceincorrectormisleadingcalculationsorothergarbage.TheCalcdocumentation
doesn'texplainwhathappensifthetableentriesarenotinascendingordersoIransomeexperiments.The
attachedspreadsheetshowsteststhatwererunwithOOo3.2andthe"Excelcompatibility"settings":

Searchcriteria=and<>mustapplytowholecellscheckedand
Enableregularexpressionsinformulasunchecked

Thetestsshowthat=VLOOKUP(1)doesnotscantableToSearchInsequentiallytolocatethelast
valuewhichdoesnotexceedvalueToSearchFor.Instead,VLOOKUPchecksthemiddlerowofthetableto
determineifitshouldcontinuethesearchinthefirsthalforthelasthalfofthetable.SoiftableToSearchInis
$A$2:$B$34,themiddleisrow(2+34)/2andVLOOKUPcomparescell$A$18withvalueToSearchFor.If
$A$18issmaller,VLOOKUPassumesthatvalueToSearchForcannotbeanywhereinthesecondhalfofthe
tableandchecksthemiddleofthefirsthalfofthetable,row(2+17)/2.Row9doesn'texistsoVLOOKUP
comparescell$A$9withvalueToSearchFortodetermineifvalueToSearchForisinthefirstquarterorthe
secondquarterofthetable.Thisprocesslocatestheresultmuchmorequicklythanasequentialscanbut
meansthatitiseffectivelyimpossibletopredictwhichelementwillbelocatedunlessthetableisin
ascendingorder.AnyelementwhichisoutofordercouldproduceanunexpectedresultfromVLOOKUP.It's
possiblethatotherversionsofAOOorOOoorLOorNeoOfficeorotherderivativesmayusedifferent
algorithms.Inanycase,using=VLOOKUP()or=VLOOKUP(1)withanunsorted
tableToSearchInisgoingtobeabadidea.

201204281443.ods
(12.64KiB)Downloaded650times

Q24.WhyamIgettingErr:502fromVLOOKUP?
A24:ThemostlikelyproblemisthatcolumnIdentifierislargerthanthenumberofcolumnsthatyou
specifiedfortableToSearchIn.ForexampleifcolumnIdentifieris4thentableToSearchInmustspecifyat
leastfourcolumns.

Q25.WhathappenswhenVLOOKUPfindsvalueToSearchForintableToSearchInbutthecorrespondingcell
incolumnIdentifierisempty?
A25:Let'sstartwiththeothercasesfirst.Ifthecorrespondingcellhasdata,thatdataisreturned,whetherit's
numericortext.Ifthecorrespondingcellhasanerrorlike#NUM!or#N/A,thaterrorisreturned.Ifthe
correspondingcellcontainsaformula,theformula'svalueisreturned,whetherit'sanumber,text,oranerror,
andevenifthevalueisatextstringoflength0.YoucanusetheISNUMBER(),ISTEXT(),andISERROR()
functionstodeterminewhatkindofvaluehasbeenreturnedbyVLOOKUP.Inallcases,theVLOOKUP
formula'scellwilltestFALSEforISBLANK()becausethecellcontainsaformula.

ThisleavesthecasewherethecorrespondingcellincolumnIdentifierisemptynodataandnoformula.In
thissituation:

IfoptionPrecisionasshownisunchecked,anemptyvalueisreturned.TheVLOOKUPformula'scell
willtestFALSEforISBLANK()[sincethecellcontainsaformula],FALSEforISNUMBER()[since
thevalueisnotnumeric],andFALSEforISTEXT()[sincethevalueisnottext].Nodatawillappear
inthecell.
IfoptionPrecisionasshownischecked,anumericzeroisreturned!TheVLOOKUPformula'scell
willtestFALSEforISBLANK()[sincethecellcontainsaformula],TRUEforISNUMBER()[since
thevalueiszero],andFALSEforISTEXT()[sincethevalueisnottext].0willappearinthecell.

NotethatquestionsarenotallowedintheTutorialssectionoftheforum.AskthemintheApplications
>Calcsection.
Mr.Programmer
OpenOffice3.2.0Build9483onMacOSX10.9.5.ThelocaleforanymenusorCalcformulasinmyposts
isEnglish(USA).

MrProgrammer
Volunteer

Posts:2821
Joined:FriJun04,20107:57pm
Location:Wisconsin,USA

Top

Displaypostsfromprevious: Allposts Sortby Posttime Ascending Go

Postareply
3postsPage1of1

ReturntoCalc

Jumpto: Calc Go
Whoisonline

Usersbrowsingthisforum:Noregisteredusersand0guests

Boardindex
TheteamDeleteallboardcookiesAlltimesareUTC+1hour[DST]

ForumpoweredbyphpBBphpBBGroup GetOpenOffice
ByanyuseofthisWebsite,youagreetobe
boundbythese
PoliciesandTermsofUse

S-ar putea să vă placă și