Link to home
Start Free TrialLog in
Avatar of hallpett
hallpett

asked on

Rewrite a table lookup function in VBA

Found the GetTableEntry function below on this site:

It works but I would like to rewrite it so I could use an actual named Excel table as the LookupTable parameter instead of a range.

It workes with this formula: =gettableentry(A1:C3,"Ben","Orange")
I would like to rewrite to work with this: =gettableentry("tblFruits","Ben","Orange")

Public Function GetTableEntry( _
            ByVal LookupTable As Range, _
            ByVal RowHeader As String, _
            ByVal ColHeader As String)

    With Application
        .Volatile
        
        With .WorksheetFunction
            GetTableEntry = LookupTable.Parent.Cells( _
                .Match(RowHeader, LookupTable.Columns(1), 0) + LookupTable.row - 1, _
                .Match(ColHeader, LookupTable.Rows(1), 0) + LookupTable.Column - 1).Value
        End With
    End With
    
End Function

Open in new window

SOLUTION
Avatar of NBVC
NBVC
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hallpett
hallpett

ASKER

NBVC, your observation gave me an idea. It workes when I change:
=gettableentry("tblFruits","Ben","Orange")
to:
=gettableentry(tblFruits[#Alle],"Ben","Orange")

( the [#Alle] is Norwegian Excel, guess it should be [#All] in English Version)

Thanks!
You're welcome
how is mine assisted, when it is the same solution as the other, and given first?
Sorry. I just share the points between you since you gave med the idea to do some more testing and MacroShadow presicely told me that there was no need for rewriting. Probably should have given you the "Accepted solution", but was not aware that this ment something.