Passing tables as parameters for Lookup

insomniac53

Board Regular
Joined
Sep 3, 2013
Messages
104
I have a series of tables on a worksheet, indentical in layout, and I need a VBA function to use a LOOKUP function for any of the tables sent to it. Simplified, the tables look like this:

Table1Table2
idcol1col2idcol1col2
1ad1ux
2be2vy
3cf3wz

<tbody>
</tbody>

My function needs to look up the number passed to the function in the id column of the given table and return data from col1 or col2 in the appropriate table. This is where I am so far:

Code:
Function TestLookup(i As Integer, s As String, j as integer)
    'i is the data in column 1, s is the table name, j needs to be 1 or 2 or whichever column I want returned.
    Dim w1 As Worksheet: 'then I set the worksheet name
    TestLookup = Application.WorksheetFunction.VLookup(i, w1.ListObjects(s).... ' and I'm stuck!!
End Function

So, if the function was sent 3, "Table2", 2 - it would return "w"
Hope that makes sense and someone can help. Thank you.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi, possibly.

Code:
Function TestLookup(i As Integer, s As String, j As Integer)
  Application.Volatile
  TestLookup = Application.WorksheetFunction.VLookup(i, Range(s & "[#All]"), j, 0)
End Function

Or using native functions:

=VLOOKUP(3,INDIRECT("Table2" & "[#All]"),2,0)
 
Upvote 0
Brilliant! I have no idea at the moment how this works, :eek: but the most important thing is that it works. :)
Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,371
Messages
6,124,529
Members
449,169
Latest member
mm424

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top