Modify macro : so the duplicated sheet as per liste and start placing adjacent o the "Master_Template" in sequence.

Chaturvedi, Santosh 450 Reputation points
2026-10-05T10:53:22.4066667+00:00

Modify the macro so that the duplicated sheet must comes as increasing order as per F20 cell in "Dash Board".

It duplicated sheets should be placed next to "Master_Template"


Sub CopyAndRenameSheets()

Dim wsDash As Worksheet

Dim wsTemplate As Worksheet

Dim wsNew 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")



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:=wsTemplate

        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

        

        outRow = outRow + 1

    End If

Next i

End Sub

Microsoft 365 and Office | Excel | For home | Windows
0 comments No comments

Answer accepted by question author

AI answer

2026-10-05T10:53:36.2666667+00:00

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:

  1. wsInsertAfter starts as Master_Template.
  2. The first copied sheet is placed right after Master_Template.
  3. wsInsertAfter is then updated to the newly created sheet.
  4. The next copied sheet is placed after that one.
  5. This continues, so all duplicated sheets stay adjacent to Master_Template in 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.


AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Chaturvedi, Santosh 450 Reputation points
    2026-10-05T10:58:14.59+00:00

    can u modify the macro so that the final lszet content in F20 formated to left side of teh table

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.