Excel udf: Looking up data in multiple cross reference tables
This post describes how to lookup values in multiple cross reference tables using a user defined function.
This custom function accepts an arbitrary number of range arguments (table ranges). The custom function returns the first match in each table.
Array formula in cell C19:C21:
How to enter array formula
- Select cell range C19:C21
- Copy paste formula into formula bar
- Press and hold Ctrl + Shift
- Press Enter once
- Release all keys
VBA Code:
Function SearchMultipleTbl(xaxis As Variant, yaxis As Variant, ParamArray cellranges() As Variant)
Dim i As Integer, x As Variant, y As Variant
Dim temp() As Variant, xrange As Range, yrange As Range
ReDim temp(0)
On Error Resume Next
For i = LBound(cellranges) To UBound(cellranges)
Set xrange = cellranges(i).Rows(1)
Set yrange = cellranges(i).Columns(1)
x = Application.WorksheetFunction.Match(xaxis, xrange, 0)
y = Application.WorksheetFunction.Match(yaxis, yrange, 0)
If Err.Number = 0 Then
temp(UBound(temp)) = cellranges(i).Rows(y).Columns(x).Value
ReDim Preserve temp(UBound(temp) + 1)
Else
On Error GoTo 0
End If
Next i
ReDim Preserve temp(UBound(temp) - 1)
SearchMultipleTbl = Application.Transpose(temp)
End FunctionWhere to copy vba code?
- Press Alt-F11 to open visual basic editor
- Click Module on the Insert menu
- Copy and paste vba code.
- Exit visual basic editor
Related posts:
Excel udf: Sumif across multiple sheets
Excel udf: Combine cell ranges into a single range while eliminating blanks




















I get errors when I change C17 and 18. And when I change D6 to "E" the results are 72,42.1 and 8.9. it appears they should be 72, 31.6, and 8.9. Why is the second number returned under results "42.1"? please help me understand.
Steve,
You are right, there was something wrong with the code.
I have changed the vba code and uploaded a new file.
Many thanks for commenting!
Wow!!! Awesome,
Thanks for sharing this extremely useful logic.