[英]Excel formula: For each instance of value in column, get value of another column in same row
I am looking to solve the following problem in Excel: 我正在寻找解决Excel中的以下问题:
ID Key Value
1 10 20
2 5 30
3 10 20
4 10 20
If key == 10 and Value == 20, get the ID. 如果key == 10并且Value == 20,则获取ID。
So, I need this to produce the following list: "1,3,4" 因此,我需要生成以下列表:“ 1、3、4”
Essentially, I'm looking to see if one value is in a given range, and another value is in another range, give me the corresponding value (same row) in another range. 本质上,我正在查看一个值是否在给定范围内,而另一个值是否在另一个范围内,请给我对应的值(同一行)在另一个范围内。
I cannot assume that the ID column will always be the left most column. 我不能认为ID列将始终是最左侧的列。
You can use the attached User Defined Function for that purpose. 为此,您可以使用随附的用户定义功能。 Call it from your worksheet as follows:
从您的工作表中调用它,如下所示:
=concatPlusIfs(A1:A4,",",1,10,2,20) = concatPlusIfs(A1:A4, “”,1,10,2,20)
where 哪里
20 is the criteria for your Value 价值的准则是20
Public Function concatPlusIfs(rng As Range, sep As String, lgCritOffset1 As Long, varCrit1 As Variant, lgCritOffset2 As Long, varCrit2 As Variant, Optional noDup As Boolean = False, Optional skipEmpty As Boolean = False) As String Dim cl As Range, strTemp As String If noDup Then 'remove duplicates, use collection to avoid them Dim newCol As New Collection On Error Resume Next For Each cl In rng.Cells If skipEmpty = False Or Len(Trim(cl.Text)) > 0 Then If cl.Offset(, lgCritOffset1) = varCrit1 And cl.Offset(, lgCritOffset2) = varCrit2 Then newCol.Add cl.Text, cl.Text End If Next For i = 0 To newCol.Count strTemp = strTemp & newCol(i) & sep Next Else For Each cl In rng.Cells If skipEmpty = False Or Len(Trim(cl.Text)) > 0 Then If cl.Offset(, lgCritOffset1) = varCrit1 And cl.Offset(, lgCritOffset2) = varCrit2 Then strTemp = strTemp & cl.Text & sep End If Next End If concatPlusIfs = Left(strTemp, Len(strTemp) - Len(sep)) End Function
I would say this is the most basic function of excel but since your assuming the artifical limitation that you can't decide how your columns are going to be ordered - then it requires you to use something like HLOOKUP (assuming you can at least determine your headers): 我想说这是excel的最基本功能,但是由于您假设了人为的限制,即您无法决定如何对列进行排序-因此,它要求您使用HLOOKUP之类的东西(假设您至少可以确定自己的列头):
=IF(AND(HLOOKUP("Key",$A$1:$C$5,ROW(),FALSE)=10,HLOOKUP("VALUE",$A$1:$C$5,ROW(),FALSE)=20),HLOOKUP("ID",$A$1:$C$5,ROW(),FALSE),"")
Good Luck. 祝好运。
EDIT/ADDITION: 编辑/添加:
Use the multicat function from: http://www.mcgimpsey.com/excel/udfs/multicat.html 使用以下网址中的multicat函数: http : //www.mcgimpsey.com/excel/udfs/multicat.html
eg Sort your table to get rid of the spaces and then: 例如,对表进行排序以除去空格,然后:
=multicat(C2:C5,",")
Or if sorting is too much work for you - you can use this modified version of the mcgimpsey function to get rid of blank cells: 或者,如果排序对您来说太繁琐了,您可以使用mcgimpsey函数的此修改版本来摆脱空白单元格:
Public Function MultiCat( _
ByRef rRng As Excel.Range, _
Optional ByVal sDelim As String = "") _
As String
Dim rCell As Range
For Each rCell In rRng
If rCell.Value <> "" Then
MultiCat = MultiCat & sDelim & rCell.Text
End If
Next rCell
MultiCat = Mid(MultiCat, Len(sDelim) + 1)
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.