复制一个隐藏的模板并放在模板 sheet 之后

Copy a hidden template and put after template sheet

我已经使用模板 sheet 创建了我的作品sheet。我还创建了一个按钮来复制模板 sheet 并将任何新创建的 sheet 放在模板 sheet 之后。

如果 'Template' 可见,则一切正常。当我隐藏 'Template' 时,只有最新创建的 sheet 可见,其他的会自动隐藏。

你能看看我的脚本并告诉我如何解决吗?我希望任何新创建的 sheet 都可见并放在隐藏模板 sheet 之后。

这是我的脚本

Sub NewSheet()
    Dim newWS As Worksheet
    Dim TemplateWS As Worksheet
    Dim newWSName As String
      
Retry:
    newWSName = InputBox("Enter Activity name.", "New Sheet Name")
    If newWSName = "" Then Exit Sub

    For Each ws In Worksheets
        If ws.Name = newWSName Then
            MsgBox newWSName & " exist already. Pick another name.", vbExclamation, "Sheet Name Exists"
            GoTo Retry
        End If

    Next ws
    
    Set TemplateWS = Sheet7
    TemplateWS.Activate
    TemplateWS.Copy After:=ActiveWorkbook.Sheets("template 1")
    ActiveSheet.Name = newWSName
    Range("A1").Select
    ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'Summary'!A1", TextToDisplay:="Click to Summary"
    ActiveSheet.Tab.ColorIndex = 19
    
End Sub

一如既往的感谢。

通常情况下,复制的 sheet 会成为活动的 sheet,但如果复制的 sheet 被隐藏,则不会发生这种情况。解决方案是让模板可见,复制然后隐藏。

Option Explicit

Sub NewSheet()

    Dim wsTemplate As Worksheet, ws As Worksheet
    Dim newWSName As String
      
Retry:
    newWSName = InputBox("Enter Activity name.", "New Sheet Name")
    If newWSName = "" Then Exit Sub

    For Each ws In ThisWorkbook.Sheets
        If ws.Name = newWSName Then
            MsgBox newWSName & " exist already. Pick another name.", vbExclamation, "Sheet Name Exists"
            GoTo Retry
        End If
    Next ws
    
    Set wsTemplate = Sheets("Template 1")
    wsTemplate.Visible = True
    wsTemplate.Copy After:=wsTemplate
    With ActiveSheet
        .Name = newWSName
        .Range("A1").Hyperlinks.Add Anchor:=.Range("A1"), Address:="", _
         SubAddress:="'Summary'!A1", _
         TextToDisplay:="Click to Summary"
        .Tab.ColorIndex = 19
    End With
    wsTemplate.Visible = False
    
End Sub