[英]Sorting a table that another excel sheet uses causes different VLOOKUP results
[英]Excel UDF for VLOOKUP(MATCH()) that uses structured table references
我正在嘗試構建一個Excel用戶定義函數VLOOKUP_NAME(value, table, column_name)
,它將實現:
VLOOKUP(value, table, MATCH(column_name, table[#Headers], FALSE), FALSE)
我如何在VBA中執行此操作? VBA似乎不接受像table[#Headers]
這樣的結構化引用。
另外,我也有興趣了解將其實現為UDF的性能影響。
此外,我這樣做的原因是用更可讀的公式替換大量丑陋的公式。 其他更好的建議是受歡迎的。
我不熟悉那種結構化的參考語法,但您是否嘗試過構建公式並使用工作表Evaluate()
方法?
這是一個簡單的例子:
Function TestEval(s1 As String, s2 As String)
TestEval = Application.Caller.Parent.Evaluate(s1 & "/" & s2)
End Function
編輯:檢查完整個結構化表格后,這似乎有效:
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.