VBA selecting table without specifying name?

mwl_y

New Member
Joined
May 23, 2024
Messages
17
Office Version
  1. 365
Platform
  1. Windows
I have a sheet with just one table. As of right now, the table is named "Main", but it will get changed later (working with queries).
Here is a test function that checks for a certain set of values within two columns of the table:
VBA Code:
Function testFunc(ByVal a As String, ByVal b As String) As Boolean
Dim tb
Dim i As Long
tb = ActiveSheet.ListObjects("Main").DataBodyRange.Resize(, 3)
For i = 1 To UBound(tb, 1)        'for each row in table,
    If tb(i, 2) = a Then        'check col 2 for a
        If tb(i, 3) = b Then    'check col 3 for b
            testFunc = True
            Exit Function
        End If
    End If 
Next       
End Function

Is there a way to modify my code so that I don't have to include the table name? I don't want to have to go to my VBA code and change every instance of the name if I were to change the table name again in the future.

Note: The sub which calls the above function will activate the sheet that "Main" is on before calling the function.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You can refer to the list object by using its index number. Since there is only one table in the worksheet, the following should work:

VBA Code:
tb = ActiveSheet.ListObjects(1).DataBodyRange.Resize(, 3)
 
Upvote 1
Solution
You can refer to the list object by using its index number. Since there is only one table in the worksheet, the following should work:

VBA Code:
tb = ActiveSheet.ListObjects(1).DataBodyRange.Resize(, 3)
Oh wow, that's perfect! Didn't realize we could use an index number.
Is the index number specific to the sheet as well? For example, if I used ActiveSheet.ListObjects(1) on a different active sheet, would it reference the first table on that sheet, or would it try to access the same table and throw an error?
 
Upvote 0
The ListObject object's parent is a Worksheet object. Therefore, each worksheet has its own ListObject object collection that could be accessed by the index number starting from 1.
For example, if I used ActiveSheet.ListObjects(1) on a different active sheet, would it reference the first table on that sheet, or would it try to access the same table and throw an error?
It will refer to the first table on that sheet - (the active sheet in this case).

Finally, you can also use Sheet1.ListObjects(1) or Worksheets("Sheet1").ListObjects(1) to access to the first table in a specific worksheet.
 
Upvote 1
The ListObject object's parent is a Worksheet object. Therefore, each worksheet has its own ListObject object collection that could be accessed by the index number starting from 1.

It will refer to the first table on that sheet - (the active sheet in this case).

Finally, you can also use Sheet1.ListObjects(1) or Worksheets("Sheet1").ListObjects(1) to access to the first table in a specific worksheet.
This is great info. Thank you so much for your help! :)
 
Upvote 0

Forum statistics

Threads
1,223,932
Messages
6,175,468
Members
452,646
Latest member
tudou

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