VBA to get and use a table name

gberg

Board Regular
Joined
Jul 16, 2014
Messages
180
Office Version
  1. 365
Platform
  1. Windows
I want to use the same VBA on multiple worksheets. Each worksheet has a table so I need to get the table name and then use that name for each worksheet

I have this to get the table name
Range("A12").Select
TableName = ActiveCell.ListObject.Name

I tried doing this but got an error
Range("TableName[[#Headers],[Area]]").Offset(1, 0).PasteSpecial Paste:=xlPasteValues

Any help would be appreciated

Thanks,

Greg
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You could just use the table object:

Code:
Range("A12").Listobject.Listcolumns("Area").DataBodyRange.Cells(1).PasteSpecial Paste:=xlPasteValues
 
Upvote 0
RoryA,

I actually don't even like the reference to cell A12 to get the table name. I'm worried that the table will shift an the A12 reference will no longer be good (still trying to figure that out).

With that I don't like referencing A12 in the solution.

I played around a bit and was able to figure the second part out with this

Range(TableName & "[[#Headers],[Area]]").Offset(1, 0).PasteSpecial Paste:=xlPasteValues

Thanks for the suggestion
 
Upvote 0
Solution
Could call the table by its index number instead if there is only one table in each sheet or if it is always the first table.

VBA Code:
Sub test2()
    Dim ws As Worksheet
    
    Sheet3.Range("A2:E2").Copy
    
    For Each ws In ThisWorkbook.Worksheets
        ws.ListObjects(1).ListRows(1).Range.PasteSpecial xlValue
    Next ws
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,690
Members
449,117
Latest member
Aaagu

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