excel宏复制隐藏的主工作表重命名并将其放在工作簿的末尾

时间:2017-03-20 17:00:28

标签: excel vba excel-vba

我正在尝试创建一个将复制名为Master的隐藏页面的宏。当用户单击名为start的工作表中的按钮时,它将打开一个输入框。用户将输入新的工作表名称。新工作表将放在名为start的工作表之后。用户可以根据需要输入尽可能多的纸张,每个纸张都放在最后。如果单击取消,则输入框需要对无效输入和结束总和进行错误检查。以下是我所拥有的。它可以工作,但只是在开始后重命名工作表。它没有使用新名称创建新工作表。感谢

    Sub Button3_Click()
 Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Sheets("Master")
    Dim newws As Worksheet, sh As Worksheet, newname
    Dim query As Long, xst As Boolean, info As String

retry:
    xst = False
    newname = Application.InputBox("Enter Inmate Name and Number.", info, , , , , , 2)
    If newname = "False" Then Exit Sub
    For Each sh In wb.Sheets
        If sh.Name = newname Then
            xst = True: Exit For
        End If
    Next
    If Len(newname) = 0 Or xst = True Then
        info = "Sheet name is invalid. Please retry."
        GoTo retry
    End If
    ws.Copy after:=ws
    Set newws = ActiveSheet: newws.Name = sh
End Sub

2 个答案:

答案 0 :(得分:0)

这可能会这样做!...

    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Sheets("Master")
    Dim NewName As String: NewName = ""
    Dim sh As Worksheet

Retry:
    NewName = Application.InputBox("Enter Inmate Name and Number.", info, NewName, , , , , 2)
    If NewName = "False" Then Exit Sub 'user shoose 'Cancel'
    For Each sh In wb.Sheets
        If NewName = sh.Name Or NewName = "" Then
            MsgBox "Sheet name is invalid. Please retry."
            GoTo Retry
            End If
        Next sh
    ws.Copy After:=ws
    With wb.Sheets("Master (2)")
        .Visible = True
        .Activate
        .Name = NewName
        End With

答案 1 :(得分:0)

更改最后三个。

Application.ScreenUpdating = false
ws.visible = xlSheetVisible
ws.Copy after:=ws
Set newws = ActiveSheet: newws.Name = sh
ws.visible = xlsheethidden
Application.ScreenUpdating = True
End Sub