Excel 2010: VLOOKUP with multiple result

為{幸葍}努か 提交于 2019-12-23 05:33:10

问题


I have the following data:

Example:

    A             B               C
EmployeeID   EmployeeName   EmployeeSalary
-------------------------------------------
E101         JAK            20000
E102         SAM            25000
E103         John           20000
E104         Shawn          30000

I have the cell H1 in which i type salary of the employee, and in the below cell that is cell H2, I2, J2 should list the employee details according to the given salary in the cell H1.

I have used VLOOKUP function for this.

For cell H2:

=IFERROR(VLOOKUP(H1,C2:A5,1,FALSE),"EmployeeID not found")    

For cell I2:

=IFERROR(VLOOKUP(H1,C2:B4,2,FALSE),"EmployeeName not found")

For cell J2:

=IFERROR(VLOOKUP(H1,C2:C4,3,FALSE),"EmployeeSalary not found")

Note: The above works fine for single result to display but when i enter 20000 it will only show single record NOT all which meet the given criteria.


回答1:


There are three ways to deal with this:

First the Formula:

I set up the Field as such which will become apparent with another method:

So in J4 I put the following formula:

=IFERROR(AGGREGATE(14,6,$C$2:INDEX(C:C,MATCH(1E+99,C:C))/($C$2:INDEX(C:C,MATCH(1E+99,C:C))=$H$2),ROW(1:1)),"")

In H4 I put:

=IF($J4<>"",INDEX(A$2:INDEX(A:A,MATCH(1E+99,$C:$C)),AGGREGATE(15,6,(ROW($C$2:INDEX($C:$C,MATCH(1E+99,$C:$C)))-1)/($C$2:INDEX($C:$C,MATCH(1E+99,$C:$C))=$J4),COUNTIF($J$4:$J4,$J4))),"")

Which I then drag across to I4. Then drag all three formulas down till you are sure you have covered all the possible results.

This is a non CSE array formula. Array formulas calculations are exponential, so we need to limit the reference range to the minimum needed. All the INDEX($C:$C,MATCH(1E+99,$C:$C)) finds the last cell with data and sets this as the end reference.

The IFERROR() wrapper on the first allows the formula to be copied down further than the list will return and avoid the #N/A. In the picture the formulas occupy the first 8 rows.


Second we use the Advanced Filter:

First we set up the area around H1 like this:

Then we navigate to Advanced Filter which is on the Data tab. This window pops open:

Then we enter the Information:

Mark the Copy to another location. List Range is $A$1:$C$5 Criteria Range is $A$1:$C$5 Copy to range is $H$3:$J$3

Then hit okay.


The third is vba which mimic the Advanced Filter:

Sub atfilt()
Dim ws As Worksheet
Dim rng As Range
Dim critrng As Range
Dim cpytorng As Range
Dim lstrow As Long

Set ws = Sheets("Sheet9")
lstrow = ws.Range("A" & ws.Rows.Count).End(xlUp).row
Set rng = ws.Range("A1:C" & lstrow)
Set critrng = ws.Range("H1:H2")
Set cpytorng = ws.Range("H3:J3")

rng.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=critrng, CopyToRange:=cpytorng, Unique:=False
End Sub

Each has their disadvantages:

Formula: If the data set is large then(1,000 rows or more) the calculations will be long.

Advanced Filter: Each step must be redone each time a new filter is wanted. It is not automatic.

VBA: It is VBA and requires a certain understanding on how to use it.




回答2:


I agree with @Scott Craner's comment that autofilter would be great here to allow you to find multiple values based on a criteria. Unfortunately (maybe someone can fill this bit in :)) I don't know much about autofilter in vba for this purpose (only used it once or twice)

I can tell you about left lookups with INDEX(MATCH()) which should work in place of your VLOOKUPS.

Format:

INDEX("column of values to return",MATCH("lookup value","column to find lookup value", 0))

so in your example for cell H2 you could use:

=IFERROR(INDEX($A:$A,MATCH(H1,$C:$C,0)),"EmployeeID not found")   

Note the "0" in the formula is to find an exact match!



来源:https://stackoverflow.com/questions/36796461/excel-2010-vlookup-with-multiple-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!