How to reference table in a worksheet

TheRedCardinal

Board Regular
Joined
Jul 11, 2019
Messages
243
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have a Sub routine (thanks to prior help) that performs a set of tasks on a worksheet when the user closes it. There are 90 possible sheets.

On each sheet are three tables, but the way they have been created means their names are not predictable.

They are always in the same place. Top left cell of the header row is A5, L5 and S5 respectively.

How can I assign them to objects in my macro, so that I can manipulate them correctly. Specifically, I will want to clear the Data of each and write back the results of an Array calculation from my Macro. I also want to highlight some rows etc.

Could I allocate them to ListObjects my reference to location?

Thanks!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Loop through the listobjects collection, and allocate each one to the relevant variable based on Listobject.Range.Cells(1).Address
 
Upvote 0
Hey Rory,
Thanks - and I love how you give the answer but leave some work to be done. Great for development!

I tried this:

VBA Code:
Dim ImportTab As Object, ExportTab As Object, SummaryTab As Object, TableTest as Object

For Each TableTest In WS1.ListObjects

    If TableTest.Range.Cells(1).Address = WS1.Range("G5") Then Set ImportTab = TableTest
    If TableTest.Range.Cells(1).Address = WS1.Range("L5") Then Set ExportTab = TableTest
    If TableTest.Range.Cells(1).Address = WS1.Range("S5") Then Set SummaryTab = TableTest

Next TableTest

It loops through without error, but doesn't set any of the tables correctly.

I'm mixing up references I think?
 
Upvote 0
You're comparing an address to a cell value. Use:

Code:
    If TableTest.Range.Cells(1).Address = "$G$5" Then
       Set ImportTab = TableTest
    ElseIf TableTest.Range.Cells(1).Address = "$L$5" Then
         Set ExportTab = TableTest
    ElseIf TableTest.Range.Cells(1).Address = "$S$5" Then
       Set SummaryTab = TableTest
    End If
 
Upvote 0
Solution
You could also use:

VBA Code:
    Select Case TableTest.Range.Cells(1).Address
       Case "$G$5"
          Set ImportTab = TableTest
       Case "$L$5" 
          Set ExportTab = TableTest
      Case "$S$5"
          Set SummaryTab = TableTest
    End Select
 
Upvote 0

Forum statistics

Threads
1,215,717
Messages
6,126,428
Members
449,314
Latest member
MrSabo83

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