Excel 带有 IF 语句和 INDEX 的自定义公式,MATCH returns #VALUE 错误
Excel custom formula with IF statements and INDEX, MATCH returns a #VALUE error
我正在从一个 Excel sheet 中提取值到另一个。我使用以下公式:
= IF(ISBLANK(IFNA(INDEX(Range1;Row;MATCH(Dates;Range2;0));Error1));Error2;IFNA(INDEX(Range1;Row;MATCH(Dates;Range2;0));Error1))
因为我需要对提取的数据进行计算,所以我想创建一个自定义函数,通过消除每次都保持相同的参数输入来简化提取过程。所以我构建了以下函数:
Function DataCap(Dates As Variant, Row As Variant) As Variant
Dim Range1 As Range 'Define the whole lookup table
Set Range1 = Range("Data!A1:P151")
Dim Range2 As Range 'Define the date lookup table
Set Range2 = Range("Data!A3:P3")
Dim Error1 As Range 'Define the error 1; if no value
Set Error1 = Range("X16")
Dim Error2 As Range 'Define the error 2; if blank
Set Error2 = Range("Y16")
DataCap.Formula "= IF(ISBLANK(IFNA(INDEX(Range1;Row;MATCH(Dates;Range2;0));Error1));Error2;IFNA(INDEX(Range1;Row;MATCH(Dates;Range2;0));Error1))"
End Function
当我 运行 它时,我得到了 #VALUE 错误。如果我将等式(具有相同的参数)直接粘贴到单元格中,它会按预期工作。所以,我认为问题是 VBA 无法进行计算。任何建议如何解决这个问题?
提前感谢您的帮助
由于无法在您的数据上进行测试,我无法确定这是否可行,但它至少可以为您指明正确的方向。
Public Function DCap(xDates As Variant, xRow As Variant)
Dim Range1 As Range, Range2 As Range, Error1 As Range, Error2 As Range
Set Range1 = ActiveWorkbook.Sheets("Data").Range("A1:P151")
Set Range2 = ActiveWorkbook.Sheets("Data").Range("A3:P3")
Set Error1 = ActiveWorkbook.ActiveSheet.Range("X16")
Set Error2 = ActiveWorkbook.ActiveSheet.Range("Y16")
On Error Resume Next
If IsError(WorksheetFunction.Index(Range1, xRow, WorksheetFunction.Match(xDates, Range2, 0))) Then
DCap = Error1
ElseIf Len(WorksheetFunction.Index(Range1, xRow, WorksheetFunction.Match(xDates, Range2, 0))) = 0 Then
DCap = Error2
Else
DCap = WorksheetFunction.Index(Range1, xRow, WorksheetFunction.Match(xDates, Range2, 0))
End If
On Error GoTo 0
End Function
我正在从一个 Excel sheet 中提取值到另一个。我使用以下公式:
= IF(ISBLANK(IFNA(INDEX(Range1;Row;MATCH(Dates;Range2;0));Error1));Error2;IFNA(INDEX(Range1;Row;MATCH(Dates;Range2;0));Error1))
因为我需要对提取的数据进行计算,所以我想创建一个自定义函数,通过消除每次都保持相同的参数输入来简化提取过程。所以我构建了以下函数:
Function DataCap(Dates As Variant, Row As Variant) As Variant
Dim Range1 As Range 'Define the whole lookup table
Set Range1 = Range("Data!A1:P151")
Dim Range2 As Range 'Define the date lookup table
Set Range2 = Range("Data!A3:P3")
Dim Error1 As Range 'Define the error 1; if no value
Set Error1 = Range("X16")
Dim Error2 As Range 'Define the error 2; if blank
Set Error2 = Range("Y16")
DataCap.Formula "= IF(ISBLANK(IFNA(INDEX(Range1;Row;MATCH(Dates;Range2;0));Error1));Error2;IFNA(INDEX(Range1;Row;MATCH(Dates;Range2;0));Error1))"
End Function
当我 运行 它时,我得到了 #VALUE 错误。如果我将等式(具有相同的参数)直接粘贴到单元格中,它会按预期工作。所以,我认为问题是 VBA 无法进行计算。任何建议如何解决这个问题?
提前感谢您的帮助
由于无法在您的数据上进行测试,我无法确定这是否可行,但它至少可以为您指明正确的方向。
Public Function DCap(xDates As Variant, xRow As Variant)
Dim Range1 As Range, Range2 As Range, Error1 As Range, Error2 As Range
Set Range1 = ActiveWorkbook.Sheets("Data").Range("A1:P151")
Set Range2 = ActiveWorkbook.Sheets("Data").Range("A3:P3")
Set Error1 = ActiveWorkbook.ActiveSheet.Range("X16")
Set Error2 = ActiveWorkbook.ActiveSheet.Range("Y16")
On Error Resume Next
If IsError(WorksheetFunction.Index(Range1, xRow, WorksheetFunction.Match(xDates, Range2, 0))) Then
DCap = Error1
ElseIf Len(WorksheetFunction.Index(Range1, xRow, WorksheetFunction.Match(xDates, Range2, 0))) = 0 Then
DCap = Error2
Else
DCap = WorksheetFunction.Index(Range1, xRow, WorksheetFunction.Match(xDates, Range2, 0))
End If
On Error GoTo 0
End Function