2

我希望在 Excel 中解决以下问题:

ID     Key     Value
1      10      20
2       5      30
3      10      20
4      10      20

如果 key == 10 且 Value == 20,则获取 ID。

所以,我需要这个来产生以下列表:“1,3,4”

本质上,我正在查看一个值是否在给定范围内,而另一个值在另一个范围内,给我另一个范围内的相应值(同一行)。

我不能假设 ID 列总是最左边的列。

4

2 回答 2

1

您可以为此目的使用附加的用户定义函数。从您的工作表中调用它,如下所示:

=concatPlusIfs(A1:A4,",",1,10,2,20)

在哪里

  • A1:A4 是 ID 列表
  • "," 是分隔符
  • 1 是您的 id 列和您的键列之间的偏移量(-1 表示左侧的 1 列)
  • 10 是您的 Key 的标准
  • 2 是您的 id 列和您的 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
    
于 2012-09-25T17:42:45.217 回答
0

我会说这是 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

祝你好运。

编辑/添加:

使用来自: http ://www.mcgimpsey.com/excel/udfs/multicat.html的 multicat 函数

例如,对表格进行排序以消除空格,然后:

=multicat(C2:C5,",")

或者,如果排序对您来说工作量太大 - 您可以使用 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
于 2012-09-25T18:03:40.950 回答