简体   繁体   English

Excel UDF for VLOOKUP(MATCH())使用结构化表引用

[英]Excel UDF for VLOOKUP(MATCH()) that uses structured table references

I'm trying to build an Excel user defined function VLOOKUP_NAME(value, table, column_name) that would implement: 我正在尝试构建一个Excel用户定义函数VLOOKUP_NAME(value, table, column_name) ,它将实现:

VLOOKUP(value, table, MATCH(column_name, table[#Headers], FALSE), FALSE)

How do I do this in VBA? 我如何在VBA中执行此操作? VBA doesn't seem to accept structured references like table[#Headers] . VBA似乎不接受像table[#Headers]这样的结构化引用。

Also, I'd be interested in knowing about the performance consequences of implementing this as a UDF as well. 另外,我也有兴趣了解将其实现为UDF的性能影响。

Also, the reason I'm doing this is to replace a ton of ugly formulas with more readable formulas. 此外,我这样做的原因是用更可读的公式替换大量丑陋的公式。 Other better suggestions to achieve this are welcome. 其他更好的建议是受欢迎的。

I'm not familiar with that structured reference syntax, but have you tried constructing the formula and using the worksheet Evaluate() method? 我不熟悉那种结构化的参考语法,但您是否尝试过构建公式并使用工作表Evaluate()方法?

Here's a simple example: 这是一个简单的例子:

Function TestEval(s1 As String, s2 As String)
    TestEval = Application.Caller.Parent.Evaluate(s1 & "/" & s2)
End Function

EDIT: After checking out the whole structured table thing, this seems to work: 编辑:检查完整个结构化表格后,这似乎有效:

Function TableLookup(val, tbl As Range, colName As String)
    Dim indx, rv
    indx = Application.Match(colName, tbl.Rows(1).Offset(-1, 0), 0)

    If Not IsError(indx) Then
        rv = Application.VLookup(val, tbl, indx, False)
        TableLookup = IIf(IsError(rv), "Not found", rv)
    Else
        TableLookup = "Col??"
    End If
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM