How to detect if user select cancel InputBox VBA Excel

后端 未结 3 970
我寻月下人不归
我寻月下人不归 2020-12-03 04:27

I have an input box asking user to enter a date. How do I let the program know to stop if the user click cancel or close the input dialog instead of press okay.

Some

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 05:04

    Following example uses InputBox method to validate user entry to unhide sheets: Important thing here is to use wrap InputBox variable inside StrPtr so it could be compared to '0' when user chose to click 'x' icon on the InputBox.

    Sub unhidesheet()
    
    Dim ws As Worksheet
    Dim pw As String
    
    pw = InputBox("Enter Password to Unhide Sheets:", "Unhide Data Sheets")
    If StrPtr(pw) = 0 Then
    
       Exit Sub
    ElseIf pw = NullString Then
       Exit Sub
    ElseIf pw = 123456 Then
        For Each ws In ThisWorkbook.Worksheets
            ws.Visible = xlSheetVisible
        Next
    End If
    End Sub
    

提交回复
热议问题