Use table name from current sheet

gberg

Board Regular
Joined
Jul 16, 2014
Messages
180
Office Version
  1. 365
Platform
  1. Windows
I have a macro that will be used on multiple sheets. Each sheet will have a Table (table will be the same size and have the same headers on each sheet). I want to run a macro that can be used on each sheet, my issue is that the tables will all have different names and I want to reference the table name for the current sheet. This is what I have tried (and failed) to come up with.

Sub Different_Tables()
Range("A1").Select
Dim ws As Worksheet
Dim tName As String

Set ws = ActiveSheet

tName = ActiveCell.ListObject.Name


Range("tName[[#Headers],[CATEGORY]]").Offset(1, 0).Select

End Sub

Hope someone can help

Thanks,

Greg
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Do you only have one table on each sheet?
 
Upvote 0
Sorry, yes. There is only one table on each sheet
 
Upvote 0
If only one (or none) per sheet try using list object index like this:
VBA Code:
Sub SelectTable()
Dim tName As String
On Error Resume Next
tName = ActiveSheet.ListObjects(1).Name
If tName <> "" Then
    MsgBox ActiveSheet.Name & vbNewLine & tName
Else
    MsgBox "No table on sheet " & ActiveSheet.Name
End If
On Error GoTo 0
End Sub
 
Upvote 0
How do I use that to reference the table? I want to select the first cell in one of the columns in the table

Range("tName[[#Headers],[CATEGORY]]").Offset(1, 0).Select
 
Upvote 0
Avoiding the unnecessary selection, perhaps
VBA Code:
With ActiveSheet.Range(tName & "[[#Headers],[CATEGORY]]").Offset(1, 0)
or maybe even
VBA Code:
With ActiveSheet.Range(tName & "[CATEGORY]")(1)
 
Upvote 0
Another option
VBA Code:
ActiveSheet.ListObjects(1).ListColumns("Category").Range(2).Select
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

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