InputBox - Cancel Loop

adambc

Active Member
Joined
Jan 13, 2020
Messages
405
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

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
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,221,525
Messages
6,160,327
Members
451,637
Latest member
hvp2262

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