Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

indexing - Excel - VLOOKUP vs. INDEX/MATCH - Which is better?

I understand how to use each method: VLOOKUP (or HLOOKUP) vs. INDEX/MATCH.

I'm looking for differences between them not in terms of personal preference, but primarily in the following areas:

  1. Is there something that one method can do that the other cannot?

  2. Which one is more efficient in general (or does it depend on the situation)?

  3. Any other advantages/disadvantages to using one method vs. the other

NOTE: I am answering my own question here but looking to see if anyone else has other insights I hadn't thought of.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I prefer to use INDEX/MATCH in practically every situation because it is far more flexible and has the potential to be much more efficient depending on how large the lookup table is.

The only time when I can really justify using VLOOKUP is for very straight-forward tables where the column index number is dynamic, although even in this case, INDEX/MATCH is equally viable.

I'll give a few specific examples below to demonstrate the detailed differences between the two methods.


INDEX/MATCH can lookup to the left (or anywhere else you want)

This is probably the most obvious advantages to INDEX/MATCH as well as one of the biggest downfalls of VLOOKUP. VLOOKUP can only lookup to the right, INDEX/MATCH can lookup from any range, including different sheets if necessary.

The example below cannot be accomplished with VLOOKUP.

enter image description here


INDEX/MATCH has the potential to use smaller cell ranges (thus increasing efficiency)

Consider the example below. It can be accomplished with either method.

enter image description here

enter image description here

Both of these formulas work fine. However, since the VLOOKUP formula contains a larger range than the INDEX/MATCH formula, it is unnecessarily volatile.

If any cell in the range B1:G4 changes, the VLOOKUP formula must recalculate (because B1:G4 is within the range A1:H4) even though changing any cell in B1:G4 will not affect the outcome of the formula. This is not an issue for INDEX/MATCH because its formula does not contain the range B1:G4.


Using VLOOKUP with fixed col_index_number is dangerous

The main issue I see with having a fixed column index number is that it will not update as it should if full columns are inserted. Consider the following example:

enter image description here

This formula works fine unless a column is inserted within the lookup table. In that case, the formula will lookup the value to the left of where it should. See below, result after a column has been inserted.

enter image description here

This can actually be alleviated by using the following VLOOKUP formula instead:

= VLOOKUP("s",A1:H4,COLUMN(H1)-COLUMN(A1)+1,FALSE)

Now H1 will automatically update to I1 if a column is inserted, thus preserving the reference to the same column. However, this is entirely unnecessary because INDEX/MATCH can accomplish this without this problem with the formula below.

= INDEX(H1:H4,MATCH("s",A1:A4,0))

I realize this is an unlikely scenario, but it always bothered me that VLOOKUP by default looks up based on a fixed column index that does not automatically update if columns are inserted. To me, it just seems to make the VLOOKUP function more fragile.


INDEX/MATCH can handle variable column indexes just as well, but longer formula

If the column index number itself is dynamic, this is really the only case when I think VLOOKUP simplifies things a bit, but again the INDEX/MATCH alternative is just as good, just slightly more confusing. See below examples.

enter image description here

enter image description here


INDEX/MATCH is more efficient for multiple lookups

(thanks to @jeffreyweir)

If multiple lookup values are needed for a single match value, it is much more efficient to have a helper cell with the match value. This way, the match only has to be computed once, instead of one for each lookup formula. See example below.

enter image description here

This match value can then be used to return the appropriate lookup values. See example below, (formula has been dragged to the right).

enter image description here

This manual "splitting" of the match value and index values is not an option with VLOOKUP since the match value is an "internal" variable in VLOOKUP and cannot be accessed.


INDEX/MATCH can look up a range, allowing another operation

Let's say for example you want to find a max value in a column based on the column name.

You can first use MATCH to find the appropriate column, then INDEX to return the range of that entire column, then use MAX to find the max of that range.

See example below, the formula in H4 looks up the max value of the column name specified in cell G4. This cannot be accomplished using VLOOKUP alone.

enter image description here


MATCH doesn't have to match an exact value

Usually MATCH is used with the third argument as 0, meaning "find an exact match". But depending on the situation, using -1 or 1 as the third argument of MATCH can be very useful.

For example, the following formula returns the row number of the last row in column A that contains a number:

= MATCH(-1E+300,A:A,-1)

This is because this formula starts from the bottom of the A column and works its way toward the top, and returns the first row number in the A column where the value is greater than or equal to -1E+300 (which is basically any number).

Then INDEX can be used in combination with this to return the value in that cell. See example below.

enter image description here


In Summary

VLOOKUP is, at best, as good as INDEX/MATCH and admittedly slightly less confusing in some situations. And at worst, VLOOKUP is much more unsafe and volatile than INDEX/MATCH.

Also worth noting that if you want to look up a range instead of a single value, INDEX/MATCH must be used. VLOOKUP cannot be used to look up a range.

For these reasons, I generally prefer INDEX/MATCH in practically all situations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...