如何在vba中关闭我的工作表功能?

时间:2015-03-19 18:03:25

标签: vba

如果我的工作表不匹配或单击取消,我该如何关闭它?如果我点击取消它继续带我回去进入工作表。这是我的代码。

   Function WorksheetExists(WSName As String) As Boolean
    On Error Resume Next
    WorksheetExists = Worksheets(WSName).Name = WSName


    On Error GoTo 0
   End Function

    Do Until WorksheetExists(shname)
     shname = InputBox("Enter sheet name")
       If Not WorksheetExists(shname) Then
         MsgBox shname & " doesn't exist!", vbExclamation

       Else
        WSName = shname

         Sheets(shname).Select
       End If
       Loop

1 个答案:

答案 0 :(得分:0)

您必须检查空白回复。它会保持循环,因为您没有空字符串作为工作表名称。

    Do Until WorksheetExists(shname)
        shname = InputBox("Enter sheet name")
        If shname = "" Then
            Exit Do
        ElseIf Not WorksheetExists(shname) Then
            MsgBox shname & " doesn't exist!", vbExclamation
        Else
            WSName = shname
            Sheets(shname).Select
        End If
    Loop

Do Until WorksheetExists(shname) Or shname = ""
    shname = InputBox("Enter sheet name")
    If Not WorksheetExists(shname) Then
        MsgBox shname & " doesn't exist!", vbExclamation
    Else
        WSName = shname
        Sheets(shname).Select
    End If
Loop