在VBA Excel中构建自定义二维查找函数

在VBA Excel中构建自定义二维查找函数,excel,vba,Excel,Vba,我有一个项目,使用该范围在VBA Excel中内置一个自定义查找函数,我尝试以不同的方式执行代码,但仍然无法得到结果。 我编写的代码是: Function lookUp1(RowTable As Range, RowValue As String, ColTable As Range, ColValue As String) As Variant Dim row As Variant Dim col As Variant For Each row In RowTable If ro

我有一个项目,使用该范围在VBA Excel中内置一个自定义查找函数,我尝试以不同的方式执行代码,但仍然无法得到结果。

我编写的代码是:

Function lookUp1(RowTable As Range, RowValue As String, ColTable As Range, ColValue As String) As Variant

Dim row As Variant
Dim col As Variant

For Each row In RowTable
    If row.Value = RowValue Then
        row.row
    End If
Next row

For Each col In ColTable
    If col = ColValue Then
        col.Column
   End If
Next col

lookUp1 = Cells(row, col)

End Function
在我附上的工作表截图中,答案应该是20美元,我一直收到一个错误值


我将非常感谢您的帮助:)

行和列在您的函数中返回为空

Function lookUp1(ByRef rowtable As Range, ByRef rowvalue As String, ByRef coltable As Range, ByRef colvalue As String) As Variant

Dim cel As Variant

Dim row As Variant
Dim col As Variant

For Each cel In rowtable
    If cel.Value = rowvalue Then
        row = cel.row
    End If
Next cel

For Each cel In coltable
    If cel = colvalue Then
        col = cel.Column
   End If
Next cel

lookUp1 = Cells(row, col).Value


End Function

变暗r为长,c为长,
r=row.row
c=c.Column
lookup1=单元格(r,c)
。。。但是您可以使用
Match
@BigBen c=Col.column是的,输入错误。。。实际上,我们不应该在这里循环。请尝试遵循上面评论中建议的最佳实践:)