Solved

Rewrite a table lookup function in VBA

Posted on 2014-01-30
6
254 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
  • 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 26

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now