VBA中的INDEX(MATCH,MATCH)(类型13类型不匹配)

时间:2019-08-07 17:20:28

标签: excel vba

我正在尝试使用INDEX(MATCH,MATCH)重新创建此公式:

INDEX(Inflation!H$129:H$188,MATCH($J10,Inflation!$C$129:$C$188,0))

您可能会注意到,列未锁定在INDEX数组字段Inflation!H$129:H$188上。我不知道如何移至一列,所以我想使用INDEX(MATCH,MATCH)

在下面的代码中,即使我将arrInflation_Bucket(I, 1)换为“ EWIP”(在搜索范围内的已知条目),也会出现错误13类型不匹配。

Sub Costs__Repossession_Costs()
    Dim I, J, arrInflation_Bucket, arrInflation_Bucket_Label, arrNumber_of_Assets, arrQuarters, arrInflation_Label_Match, arrInflation_Quarter_Match

    arrNumber_of_Assets = Range("Costs.Number_of_Assets")
    arrQuarters = Range("Quarters_1to40")
    arrInflation_Bucket = Range("Costs.Inflation_Bucket")
    arrInflation_Bucket_Label = Range("Inflation.Inflation_Bucket_Label")

    ReDim arrCosts__Repossession_Costs(1 To UBound(arrNumber_of_Assets, 1), 1 To UBound(arrQuarters, 2))

    For I = LBound(arrInflation_Bucket, 1) To UBound(arrInflation_Bucket, 1)
        For J = LBound(arrQuarters, 2) To UBound(arrQuarters, 2)

            arrInflation_Label_Match(I, J) = Application.Match(arrInflation_Bucket(I, 1), Range("Inflation.Inflation_Bucket_Label"), 0)

            arrInflation_Quarter_Match(I, J) = Application.WorksheetFunction.Match(arrQuarters(1, J), Range("Quarters_1to40"), 0)

            arrCosts__Repossession_Costs(I, J) = Application.WorksheetFunction.Index(Range("Inflation.Cumulative"), arrInflation_Label_Match, arrInflation_Quarter_Match)

        Next J
    Next I
End Sub

到目前为止,这是我在VBA中需要做的最复杂的公式,因此,如果我能够理解这一点,那么应该一切顺利。

好的,我已经用前两个注释的说明更新了代码,非常有帮助。现在,它正确执行了两次匹配(以前是在第一次出错时使用,但现在在第48行和第1列中给出了正确的信息),但现在在INDEX()上键入13错误。

    Sub Costs__Repossession_Costs()

Dim I As Long
Dim J As Long
Dim arrInflation_Bucket As Variant

Dim arrInflation_Bucket_Label, arrNumber_of_Assets, arrQuarters, arrInflation_Label_Match, arrInflation_Quarter_Match, arrInflation_Cumulative

arrNumber_of_Assets = Range("Costs.Number_of_Assets")
arrQuarters = Range("Quarters_1to40")
arrInflation_Bucket = Range("Costs.Inflation_Bucket")
arrInflation_Bucket_Label = Range("Inflation.Inflation_Bucket_Label")
arrInflation_Cumulative = Range("Inflation.Cumulative")

ReDim arrCosts__Repossession_Costs(1 To UBound(arrNumber_of_Assets, 1), 1 To UBound(arrQuarters, 2)), _
    arrInflation_Label_Match(1 To UBound(arrNumber_of_Assets, 1), 1 To UBound(arrQuarters, 2)), _
    arrInflation_Quarter_Match(1 To UBound(arrNumber_of_Assets, 1), 1 To UBound(arrQuarters, 2))

   For I = LBound(arrInflation_Bucket, 1) To UBound(arrInflation_Bucket, 1)
      For J = LBound(arrQuarters, 2) To UBound(arrQuarters, 2)

         arrInflation_Label_Match(I, J) = Application.Match(arrInflation_Bucket(I, 1), Range("Inflation.Inflation_Bucket_Label"), 0)

         arrInflation_Quarter_Match(I, J) = Application.WorksheetFunction.Match(arrQuarters(1, J), Range("Quarters_1to40"), 0)

         arrCosts__Repossession_Costs(I, J) = Application.WorksheetFunction.Index(arrInflation_Cumulative, arrInflation_Label_Match, arrInflation_Quarter_Match)

      Next J
   Next I
End Sub

1 个答案:

答案 0 :(得分:0)

好吧,所以我像个不知所措的人崩溃了,并向我的天才朋友寻求帮助。他出色的代码如下:

Function firstMatchInRow(ByVal val, arr, ByVal col As Long) As Long
    Dim i As Long
    For i = LBound(arr, 1) To UBound(arr, 1)
        If arr(i, col) = val Then firstMatchInRow = i: Exit Function
    Next i
End Function

Sub Costs__Repossession_Costs()

    Dim i As Long, j As Long, tempLabelMatch As Long
    Dim arrInflation_Bucket, arrInflation_Bucket_Label, arrNumber_of_Assets, arrQuarters, arrInflation_Cumulative, arrCosts__Repossession_Costs

    arrQuarters = Range("Quarters_1to40")
    arrInflation_Bucket = Range("Costs.Inflation_Bucket").Value
    arrInflation_Bucket_Label = Range("Inflation.Inflation_Bucket_Label").Value
    arrInflation_Cumulative = Range("Inflation.Cumulative").Value ' + add values

    ReDim arrCosts__Repossession_Costs(1 To UBound(arrInflation_Cumulative, 1), 1 To UBound(arrQuarters, 2))

       For i = LBound(arrInflation_Bucket, 1) To UBound(arrInflation_Bucket, 1)
            tempLabelMatch = firstMatchInRow(arrInflation_Bucket(i, 1), arrInflation_Bucket_Label, 1)
            For j = LBound(arrInflation_Cumulative, 2) To UBound(arrInflation_Cumulative, 2)
                arrCosts__Repossession_Costs(i, j) = arrInflation_Cumulative(tempLabelMatch, j)
            Next j
       Next i
End Sub