如何为多变量索引匹配公式创建UDF

时间:2019-04-16 15:30:58

标签: excel vba indexing match user-defined-functions

我想为以下Excel索引匹配公式创建UDF:

{=INDEX($A$1:$J$7,MATCH(B9,$A$1:$A$7,0),MATCH(1,($A$1:$J$1=B10)*($A$2:$J$2=B11),0))}

enter image description here

    AA  AA  AA  BB  BB  BB  CC  CC  CC
    a   b   c   a   b   c   a   b   c
1   10  55  24  48  95  19  5   28  65
2   16  48  3   62  46  50  59  80  17
3   63  47  19  23  67  26  14  16  9
4   55  91  4   55  72  79  27  39  50
5   75  53  7   42  45  19  58  41  12

Condition1  3                               
Condition2  AA                              
Condition3  c                               

index-Match 19  =INDEX($A$1:$J$7,MATCH(B9,$A$1:$A$7,0),MATCH(1,($A$1:$J$1=B10)*($A$2:$J$2=B11),0))}                         

注意:CTRL + SHIFT + ENTER将返回Excel中的数组结果

这在excel工作表中有效,但是当我尝试将其转换为VBA时,出现错误。

我认为我需要在以下代码中的某个地方应用FormulaArray,非常感谢您的帮助。

我尝试使用以下代码,但获得了#VALUE!

Public Function UDF_IndexMatch(Condition1, Condition2, COndition3)
    UDF_IndexMatch = Application.WorksheetFunction.Index(Range("$A$1:$J$7"), _
                                                  Application.WorksheetFunction.Match(Condition1, Range("$A$1:$A$7"), 0), _ 
                                                 Application.WorksheetFunction.Match(1, (Range("$A$1:$J$1") = Condition2) * (Range("$A$2:$J$2") = COndition3), 0))

End Function

最终结果应该是这样的:

= UDF_IndexMatch(Condition1, Condition2, COndition3) 

返回:对应的索引匹配结果

2 个答案:

答案 0 :(得分:2)

您可以将数据保存在变量数组中并对其进行迭代:

Public Function UDF_IndexMatch(Condition1, Condition2, COndition3)
    Application.Volatile

    Dim rngarr As Variant
    rngarr = Application.Caller.Parent.Range("A1:J7").Value

    Dim irow As Long
    irow = 0

    Dim i As Long
    For i = LBound(rngarr, 1) To UBound(rngarr, 1)
        If rngarr(i, 1) = Condition1 Then
            irow = i
            Exit For
        End If
    Next i

    Dim jcolumn As Long
    jcolumn = 0

    If irow > 0 Then
        Dim j As Long
        For j = LBound(rngarr, 2) To UBound(rngarr, 2)
            If rngarr(1, j) = Condition2 And rngarr(2, j) = COndition3 Then
                jcolumn = j
            End If
        Next j
    End If

    If j > 0 Then

        UDF_IndexMatch = rngarr(irow, jcolumn)
    End If
End Function

enter image description here

答案 1 :(得分:2)

我一开始误读了您的文章,以为您最好使用标准公式,但是您正在寻找UDF。只是为了给出其他选择,这里是一个:

Public Function ReturnVal(RNG As Range, Con1, Con2, Con3) As String
Dim SearchRng1 As String, SearchRng2 As String, SearchRng3 As String, SearchRng4 As String

With ActiveWorkbook.Sheets(RNG.Parent.Name)
    SearchRng1 = RNG.Parent.Name & "!" & RNG.Range(.Cells(3, 2), .Cells(RNG.Rows.Count, RNG.Columns.Count)).Address(False, False)
    SearchRng2 = RNG.Parent.Name & "!" & RNG.Range(.Cells(3, 1), .Cells(RNG.Rows.Count, 1)).Address(False, False)
    SearchRng3 = RNG.Parent.Name & "!" & RNG.Range(.Cells(1, 2), .Cells(1, RNG.Columns.Count)).Address(False, False)
    SearchRng4 = RNG.Parent.Name & "!" & RNG.Range(.Cells(2, 2), .Cells(2, RNG.Columns.Count)).Address(False, False)
End With
ReturnVal = Evaluate("=INDEX(" & SearchRng1 & ",MATCH(" & Con1 & "," & SearchRng2 & ",0),MATCH(""" & Con2 & """&""" & Con3 & """," & SearchRng3 & "&" & SearchRng4 & ",0))")

End Function

只要您在变量中选择范围,表的位置或大小都无所谓。您也可以从另一个工作表中调用它。

enter image description here