InputBox - Cancel Loop

adambc

Active Member
Joined
Jan 13, 2020
Messages
412
Office Version
  1. 365
Platform
  1. Windows
I know how to loop if the user doesn't enter anything begore pressing OK on the InputBox ...

And I know how to Exit Sub if the user presses Cancel ...

But I want to combine the two ie if at any point in the loop the user decides they want to cancel (eg they can't find the value they need) then Exit Sub is invoked ...

Getting in a bit of a mess because both involve = FALSE!

Thanks ...
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
See if this would help

VBA Code:
Sub Test()
  Dim sCode As Variant

  sCode = Application.InputBox("Enter value")
  If TypeName(sCode) = "Boolean" Then
    'Cancel or the top right X has been clicked
  ElseIf Len(sCode) = 0 Then
    'Blank entry has been made
  Else
    'Entry has been made
  End If
End Sub
 
Upvote 0
Cancel would = False
No entry and click OK would = Empty or = ""

VBA Code:
Sub test()
    Dim n As Variant
    Do
        n = Application.InputBox("Enter something", "This is an example", Type:=2)
        If n = False Then Exit Sub ' User canceled then exit sub
        If n = Empty Then Exit Do ' User entered nothing then exit loop
        
        'Test n here
    Loop
    MsgBox "User entered nothing"
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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