Forum

VBA Code to rename ...
 
Notifications
Clear all

VBA Code to rename new Excel table

3 Posts
2 Users
0 Reactions
382 Views
 TimC
(@timc)
Posts: 32
Trusted Member
Topic starter
 

Hello,

 

In VBA I want to copy a worksheet with a table to the same workbook, and then

rename the new worksheet and table. Renaming the worksheets easy enough, but

I'm not so sure about the table. When I record a macro to copy the worksheet

the table name tblDrugsTracker is copied to tblDrugsTracker7. How can my VBA code

anticipate that the copied table will be called tblDrugsTracker7?

 

For that matter, why does macro recording not appear to generate any VBA code

to do with tables?

 
Posted : 30/09/2016 5:41 am
(@db325)
Posts: 19
Active Member
 

The table will appear in the ListObjects collection.

You should be able to loop through all the ListObjects on the new sheet to determine the name(s).

By comparing with the name(s) on the original sheet you should be able to identify the new name(s) and then rename as required.

For example, something like the following:

Dim objX As Object

For Each objX In ActiveSheet.ListObjects

MsgBox objX.Name

' add code here to compare & rename

Next objX

 
Posted : 02/10/2016 7:31 pm
 TimC
(@timc)
Posts: 32
Trusted Member
Topic starter
 

Thanks for this, Derek.

 
Posted : 04/10/2016 4:44 am
Share: