2

私はExcelで次の問題を解決しようとしています:

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

キー==10および値==20の場合、IDを取得します。

したがって、次のリストを作成するには、これが必要です: "1,3,4"

基本的に、ある値が特定の範囲内にあり、別の値が別の範囲内にあるかどうかを確認するために、別の範囲内の対応する値(同じ行)を取得します。

ID列が常に左端の列になるとは限りません。

4

2 に答える 2

1

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)

where

  • A1:A4 is the ID list
  • "," is the separator
  • 1 is the offset between your id column and your key column (-1 for 1 column to the left)
  • 10 is the criteria for your Key
  • 2 is the offset between your id column and your Value column
  • 20 is the criteria for your Value

    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

幸運を。

編集/追加:

次の multicat 関数を使用します: http://www.mcgimpsey.com/excel/udfs/multicat.html

たとえば、テーブルを並べ替えてスペースを取り除き、次のようにします。

=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 に答える