End Sub if InputBox cancelled (False)

  • Thread starter Thread starter Francis Hookham
  • Start date Start date
F

Francis Hookham

Input Box Cancel button should stop the subroutine.



OK button tests to see if 'x' has been changed.



I cannot see what is wrong here:



LastUsedRow = Sheets("Pages").Cells(Rows.Count, 5).End(xlUp).Row

DoorNum = Cells(LastUsedRow, 5)

x = InputBox("Door number?", _

"Additional items to door", DoorNum, 1)

If x = False Then

End

ElseIf x <> DoorNum Then

DoorNum = x

End If



Francis Hookham
 
Input Boxes return strings, not T/F. A Cancelled Input box returns an empty
string: ""

Select Case x
Case ""
' User Canceled
Exit Sub
Case DoorNum
' x hasn't changed: do nothing
Case Else
' x has changed
DoorNum = x
End select
 
I think that when you cancel an input box, a zero length string is returned.
As such, replace this:

If x = False Then

with this:

If x = "" Then

-Cory
 
If you use Application.InputBox the Cancel will = False
but with InputBox Function, Cancel = ""
 
Many thanks George and Cory - all's well now

I was trying to use False because I read in VBA Help - InputBox Remarks:

If you choose the OK button, InputBox returns the value entered in the
dialog box. If you click the Cancel button, InputBox returns False.

Francis Hookham
 
The Entry in Excel's Help for the Application.InputBox method says that, but
the entry for the VBA InputBox function (in both Access and Excel) says:

"If the user clicks Cancel, the function returns a zero-length string ("")."

At least, that's what I am seeing (using Office 2003)

Not specifying Application means the VBA function will be used, not the
Excel method.

Be careful which Help entries you are reading. Very similar objects,
properties etc. can really be different under the surface across apps.
*Usually* those differences are confined to how a few functions calculate or
Forms (Access Forms live in their own little universe when compared to VB
forms or Office Forms), but there are a few other "gotchas". This is
evidently one of them.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top