VBA code to Rename tables on duplicated sheet with sheet name appended

TheMacroNoob

Board Regular
Joined
Aug 5, 2022
Messages
52
Office Version
  1. 365
Platform
  1. Windows
Hello excel experts,

I have a VBA code via the magic of ChatGPT:

VBA Code:
Sub DuplicateTemplateSheet()
Dim templateSheet As Worksheet
Dim newSheet As Worksheet
Dim sheetList As Worksheet
Dim sheetName As String
Dim lastRow As Long
Dim i As Long
Dim tbl As ListObject

' Set a reference to the Template sheet
Set templateSheet = ThisWorkbook.Sheets("Template")

' Set a reference to the SheetList sheet
Set sheetList = ThisWorkbook.Sheets("SheetList")

' Find the last row of the SheetList sheet
lastRow = sheetList.Cells(sheetList.Rows.Count, "B").End(xlUp).Row

' Loop through the list of sheet names in column B of the SheetList sheet
For i = 2 To lastRow
sheetName = sheetList.Cells(i, "B").Value

' Duplicate the Template sheet and rename the copy with the current sheet name
templateSheet.Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Set newSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
newSheet.Name = sheetName

' Loop through all of the tables on the new sheet
For Each tbl In newSheet.ListObjects
  ' Rename the current table to include the sheet name
  tbl.Name = tbl.Name & "_" & sheetName
Next tbl

Next i

End Sub

There is one problem. The last directive to rename the tables doesn't really work as intended. For example, one of the tables on the 'Template' sheet is "Invoices_Paid". I would expect the duplicated sheet, correctly named "Berry", would have the new table name "Invoices_Paid_Berry", but instead, it is named "Invoices_Paid217_Berry". Numbers are added when you duplicate a table, so I'm wondering how to get around this. Is there a way to store the original table names from "Template" sheet, and then rename based on those values? Or something even easier?

I appreciate any help you can offer.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Please try this

VBA Code:
Sub DuplicateTemplateSheet()
  Dim templateSheet As Worksheet
  Dim newSheet As Worksheet
  Dim sheetList As Worksheet
  Dim sheetName As String
  Dim lastRow As Long
  Dim i As Long
  Dim tbl As ListObject
  Dim TblName As String
  Dim X As Long
  Dim Nums As String
  Dim Str As String
  
  Nums = "0123456789"
  
  ' Set a reference to the Template sheet
  Set templateSheet = ThisWorkbook.Sheets("Template")

  ' Set a reference to the SheetList sheet
  Set sheetList = ThisWorkbook.Sheets("SheetList")

  ' Find the last row of the SheetList sheet
  lastRow = sheetList.Cells(sheetList.Rows.Count, "B").End(xlUp).Row

  ' Loop through the list of sheet names in column B of the SheetList sheet
  For i = 2 To lastRow
    sheetName = sheetList.Cells(i, "B").Value

    ' Duplicate the Template sheet and rename the copy with the current sheet name
    templateSheet.Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set newSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    newSheet.Name = sheetName
    
    ' Loop through all of the tables on the new sheet
    For Each tbl In newSheet.ListObjects
      ' Rename the current table to include the sheet name
      TblName = tbl.Name
      For X = Len(TblName) To 1 Step -1
        Str = Mid(TblName, X, 1)
        If InStr(Nums, Str) > 0 Then
          TblName = Left(TblName, X - 1)
        Else
          Exit For
        End If
      Next X
          
      tbl.Name = TblName & "_" & sheetName
    Next tbl
  
  Next i


End Sub
 
Upvote 0
Solution
Please try this

VBA Code:
Sub DuplicateTemplateSheet()
  Dim templateSheet As Worksheet
  Dim newSheet As Worksheet
  Dim sheetList As Worksheet
  Dim sheetName As String
  Dim lastRow As Long
  Dim i As Long
  Dim tbl As ListObject
  Dim TblName As String
  Dim X As Long
  Dim Nums As String
  Dim Str As String
 
  Nums = "0123456789"
 
  ' Set a reference to the Template sheet
  Set templateSheet = ThisWorkbook.Sheets("Template")

  ' Set a reference to the SheetList sheet
  Set sheetList = ThisWorkbook.Sheets("SheetList")

  ' Find the last row of the SheetList sheet
  lastRow = sheetList.Cells(sheetList.Rows.Count, "B").End(xlUp).Row

  ' Loop through the list of sheet names in column B of the SheetList sheet
  For i = 2 To lastRow
    sheetName = sheetList.Cells(i, "B").Value

    ' Duplicate the Template sheet and rename the copy with the current sheet name
    templateSheet.Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Set newSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    newSheet.Name = sheetName
   
    ' Loop through all of the tables on the new sheet
    For Each tbl In newSheet.ListObjects
      ' Rename the current table to include the sheet name
      TblName = tbl.Name
      For X = Len(TblName) To 1 Step -1
        Str = Mid(TblName, X, 1)
        If InStr(Nums, Str) > 0 Then
          TblName = Left(TblName, X - 1)
        Else
          Exit For
        End If
      Next X
         
      tbl.Name = TblName & "_" & sheetName
    Next tbl
 
  Next i


End Sub
That appears to have worked beautifully. Thank you very much! Amazing!
 
Upvote 0

Forum statistics

Threads
1,224,891
Messages
6,181,614
Members
453,057
Latest member
LE102024

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