Solved

Rewrite a table lookup function in VBA

Posted on 2014-01-30
6
267 Views
Last Modified: 2014-01-30
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

0
Comment
Question by:hallpett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 23

Assisted Solution

by:NBVC
NBVC earned 250 total points
ID: 39820604
Just a quick observation... tblFruits should not be in quotes if it is a named range.
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 250 total points
ID: 39820620
No need to rewrite anything, just pass the table name.

i.e. GetTableEntry(tblFruits, "Ben", "Orange")
0
 

Author Comment

by:hallpett
ID: 39820622
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!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:NBVC
ID: 39820625
You're welcome
0
 
LVL 23

Expert Comment

by:NBVC
ID: 39820684
how is mine assisted, when it is the same solution as the other, and given first?
0
 

Author Comment

by:hallpett
ID: 39820716
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.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question