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:


<tbody>
[TD="class: xl65"]Table1[/TD]
[TD="class: xl65"][/TD]
[TD="class: xl65"][/TD]
[TD="class: xl65"]Table2[/TD]
[TD="class: xl65"][/TD]
[TD="class: xl65, width: 38"][/TD]

[TD="class: xl72"]id[/TD]
[TD="class: xl73"]col1[/TD]
[TD="class: xl74"]col2[/TD]
[TD="class: xl72"]id[/TD]
[TD="class: xl73"]col1[/TD]
[TD="class: xl74"]col2[/TD]

[TD="class: xl66"]1[/TD]
[TD="class: xl67"]a[/TD]
[TD="class: xl68"]d[/TD]
[TD="class: xl66"]1[/TD]
[TD="class: xl67"]u[/TD]
[TD="class: xl68"]x[/TD]

[TD="class: xl66"]2[/TD]
[TD="class: xl67"]b[/TD]
[TD="class: xl68"]e[/TD]
[TD="class: xl66"]2[/TD]
[TD="class: xl67"]v[/TD]
[TD="class: xl68"]y[/TD]

[TD="class: xl69"]3[/TD]
[TD="class: xl70"]c[/TD]
[TD="class: xl71"]f[/TD]
[TD="class: xl69"]3[/TD]
[TD="class: xl70"]w[/TD]
[TD="class: xl71"]z[/TD]

</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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
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, :eeek: but the most important thing is that it works. :)
Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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