简体   繁体   中英

Excel - VLOOKUP with multiple criteria

I'm currently trying to analyse an asset inventory spreadsheet consisting of PC's and Monitors. The problem I have at the moment is associating a PC with its two monitors.

For example the spreadsheet looks like this:

Sheet1 - List of PC's

Monitor 1                    Monitor 2  

Employee Name    PC Tag Number      Serial ID     MonTag1 Tag  Serial ID       MonTag2 Tag  Serial ID
John Smith         ABC123456      123456789     ABC123423        5465614         

Sheet 2 - All assets:

Parent Asset (Tag)  Tag Number    Serial ID         Description       
ABC123456            ABC123423     5465614           24" Monitor
ABC123456            XYZ123424     5456415           24" Monitor

Where Parent Asset (Tag) = Computer Bar-code

I have done a VLOOKUP of the PC Tag Number cell against sheet2 to find the tag number of the monitor attached to that PC. I then did a VLOOKUP of the tag number against sheet2 to find the serial number.

I now need to do another VLOOKUP for Monitor 2 information, I require the formula to look up the parent asset similar to the first VLOOKUP but give the value of the 2nd monitor in sheet2 instead (XYZ123424).

EG

Monitor 1                    Monitor 2  

Employee Name    PC Tag Number      Serial ID     MonTag1 Tag  Serial ID       MonTag2 Tag  Serial ID
John Smith         ABC123456      123456789     ABC123423        5465614       XYZ123424  5456415 

OK, I couldn't think of how to type out my solution, so here's a sample file: https://app.box.com/s/4j79fu7vd93u0dc1rltg

I have to say, I tend to prefer using INDEX and MATCH to VLOOKUP, so that combo (plus OFFSET) what I've used for this solution.

Hope this is what you had in mind... and please forgive the name of John Smith's Co-worker. I just had to come up with something quick to make sure it'd work with more employees and a shuffled Equipment List. :D

The link in the accepted answer has apparently expired so perhaps it is time to update this posting.

I'm not sure whether addressing multiple returns or multiple criteria is more important but building an INDEX formula with the newer AGGREGATE¹ function supplying the matching row numbers can easily handle both.

The AGGREGATE function produces cyclic calculation much as the SUMPRODUCT function does but also suffers from calculation lag when full column references are applied. For that reason, all lookup ranges will be dynamically built by picking one column and determining the beginning and end of the data. This column's end row will be used for all other lookup columns as the end point of their own columns.

The primary lookup column on Sheet2 (see sample data image below) is column A; eg [Parent Asset Tag]. The values in this column are text based so the following will determine the extents of the data.

Sheet2!$A$1:INDEX(Sheet2!$A:$A, MATCH("zzz", Sheet2!$A:$A))

If the [Parent Asset Tag] column had been numeric, this formula would be,

Sheet2!$A$1:INDEX(Sheet2!$A:$A, MATCH(1E+99, Sheet2!$A:$A))

Sheet 2 - All assets:

vlookup_multiple_criteria_data

Sheet1 - List of PC's

vlookup_multiple_criteria_formulas

The AGGREGATE function returns the appropriate row number to INDEX using its SMALL sub-function (eg 15 ). To return the second, third, etc. matching row, all that is required is raising the SMALL's k parameter.

The matching row numbers are retrieved by forcing any non-matching row numbers into a #DIV/0! error state and ignoring errors with the 6 option.

Sheet1 - Formulas

The formulas in A2 is,

=IFERROR(INDEX(Sheet2!$D:$D, AGGREGATE(15, 6, ROW(Sheet2!$A$1:INDEX(Sheet2!$A:$A, MATCH("zzz", Sheet2!$A:$A)))/(Sheet2!$A$1:INDEX(Sheet2!$A:$A, MATCH("zzz", Sheet2!$A:$A))=$B6), COLUMN(A:A))), "")

Fill right to return the second monitor listing. This is achieved by using the COLUMN function . COLUMN(A:A) returns 1 but filled right becomes COLUMN(B:B) which returns 2 . This feeds the SMALL's k parameter.

The formulas in D6 is,

=IFERROR(INDEX(Sheet2!B:B, AGGREGATE(15, 6, ROW(Sheet2!$A$1:INDEX(Sheet2!$A:$A, MATCH("zzz", Sheet2!$A:$A)))/(Sheet2!$A$1:INDEX(Sheet2!$A:$A, MATCH("zzz", Sheet2!$A:$A))=$B6), 1)), "")

That retrieves the first [MonTag1 Tag]. Fill right to E6 to pick up the first [Serial ID]. Copy the formula from D6 using the formula bar (don't copy the cell) to F6 as,

=INDEX(Sheet2!B:B, AGGREGATE(15, 6, ROW(Sheet2!A$1:INDEX(Sheet2!A:A, MATCH("zzz", Sheet2!A:A)))/(Sheet2!A$1:INDEX(Sheet2!A:A, MATCH("zzz", Sheet2!A:A))=$B6), 2))

Change the k parameter to 2 . This retrieves the second monitor's [MonTag1 Tag]. Fill right to G6 to pick up the second [Serial ID].

Nothing to it. Multiple criteria columns can be added as TRUE/FALSE statements multiplied against the denominator that produces the #DIV/0! errors.


¹ The AGGREGATE function was introduced with Excel 2010. It is not available in earlier versions.
² The IFERROR function was introduced with Excel 2007. It is not available in earlier versions.

Compatibility with earlier Excel version can be maintained with the Microsoft Office Compatibility Pack for Word, Excel, and PowerPoint .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM