Capturing Cancel of Inputbox?

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
921
Office Version
  1. 365
Platform
  1. Windows
I have an inputbox where I ask user to input something. I don't accept a null string, but I have to see if he pressed cancel. How do I capture the cancel event? Right now my code looks like:

Code:
    Do While strTest = vbNullString
        strTest= InputBox("Input something", "I ask")
        If strTest= vbNullString Then
            Call MsgBox("Can't input nothing!", vbCritical, "I ask")
        ElseIf strTest= vbCancel Then
            Exit Sub
        Else
            Exit Do
        End If
    Loop

And pressing cancel result in an error or gives that "Can't input nothing!" message. How to fix it?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi

If you use the Application.Inputbox method rather than the Inputbox function then you will get your string holding "False" if user presses cancel rather than just an empty string.
 
Upvote 0
Try like this

Code:
Sub test()
Dim inpu As Variant
inpu = Application.InputBox("Enter something")
If inpu = False Then
    MsgBox "cancelled"
ElseIf inpu = "" Then
    MsgBox "nothing entered"
Else
    MsgBox inpu
End If
End Sub
 
Upvote 0
Very good, thanks to both of you!

I have never thought that inputbox and application.inputbox would be something different. Well, you always learn something new!
 
Upvote 0
You can still use the standard VBA InputBox function and tell if the user pressed the cancel Button by checking if the StrPtr of the Input String is 0.

Something like this :

Code:
Sub Test()
 
    Dim sInput As String
    
    Do
    
        sInput = InputBox("Input something", "I ask")
        
        Select Case True
        
            Case StrPtr(sInput) = 0 [B][COLOR=SeaGreen]'===> cancel[/COLOR][/B].
            
                MsgBox "You Cancelled."
                Exit Sub
            
            Case sInput = vblullstring [COLOR=SeaGreen] [B]'===> No data Input[/B][/COLOR].
            
                Call MsgBox("Can't input nothing!", vbCritical, "I ask")
            
            Case Else    [B][COLOR=SeaGreen]'===> Data input[/COLOR][/B].
            
                Exit Do
            
            End Select
        
    Loop

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top