简体   繁体   English

Excel公式:对于列中值的每个实例,获取同一行中另一列的值

[英]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 哪里

  • A1:A4 is the ID list A1:A4是ID列表
  • "," is the separator “,”是分隔符
  • 1 is the offset between your id column and your key column (-1 for 1 column to the left) 1是您的id列和键列之间的偏移量(-1代表左侧的1列)
  • 10 is the criteria for your Key 密钥的标准是10
  • 2 is the offset between your id column and your Value column 2是您的id列和“值”列之间的偏移量
  • 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),"")

HLOOKUP_EXAMPLE

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.

相关问题 Excel 公式:对于列中峰值/谷值的每个实例,获取到下一个峰值/谷值的范围/距离 - Excel formula: For each instance of peak/bottom value in column, get range/distance to the second next peak/bottom Excel公式将行中的值移动到列 - excel formula to move value in a row to column Excel公式Vlookup通过引用行和列的值 - Excel Formula Vlookup the value by referring Row and Column Excel-在另一列中找到匹配项的行中获取列的值 - Excel - Get value of a column in a row where match found in another column Excel 公式:在列中搜索另一个值(LIKE) - Excel formula: search in column by another value (LIKE) 用Excel中的每个行值求和一个列值 - Summing a column value with each row value in excel 如何获得Excel工作表的每一行和每一列的值 - How to get the excel sheet each row and column value 从与另一行中的单元格相对应的行中获取值(在同一列中) - Get value from a row that corresponds (in the same column) to a cell in another row 如何在excel中创建一个公式来搜索行中的值,然后从该列中的另一个单元格中获取值? - How do I create a formula in excel that searches for a value in row, and then takes the value from another cell in that column? 如果另一个值在 Excel 中的同一行中,则显示值的公式是什么? - What is the formula for displaying a value if another value is in the same row in Excel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM