A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Use the copied sheet as the new insertion point each time. That places every new duplicate immediately after the previous one, so the sheets stay together next to Master_Template in the same order as the list starting at C20. The hyperlinks written to column F will also remain in that same sequence.
Sub CopyAndRenameSheets()
Dim wsDash As Worksheet
Dim wsTemplate As Worksheet
Dim wsNew As Worksheet
Dim wsInsertAfter As Worksheet
Dim lastRow As Long
Dim i As Long
Dim newName As String
Dim outRow As Long
Set wsDash = ThisWorkbook.Worksheets("Dash Board")
Set wsTemplate = ThisWorkbook.Worksheets("Master_Template")
Set wsInsertAfter = wsTemplate
lastRow = wsDash.Cells(wsDash.Rows.Count, "C").End(xlUp).Row
outRow = 20
For i = 20 To lastRow
newName = Trim(wsDash.Cells(i, "C").Value)
If newName <> "" Then
wsTemplate.Copy After:=wsInsertAfter
Set wsNew = ActiveSheet
wsNew.Name = newName
wsDash.Cells(outRow, "F").Value = newName
wsDash.Hyperlinks.Add _
Anchor:=wsDash.Cells(outRow, "F"), _
Address:="", _
SubAddress:="'" & newName & "'!A1", _
TextToDisplay:=newName
Set wsInsertAfter = wsNew
outRow = outRow + 1
End If
Next i
End Sub
How this works:
-
wsInsertAfterstarts asMaster_Template. - The first copied sheet is placed right after
Master_Template. -
wsInsertAfteris then updated to the newly created sheet. - The next copied sheet is placed after that one.
- This continues, so all duplicated sheets stay adjacent to
Master_Templatein increasing list order.
If the required order is based on the values already written in F20:F... instead of C20:C..., the loop range should be changed to read column F instead of column C.