#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default vbCancel

Hello,

I am using an input box and would like to exit the sub if
the user clicks cancel.

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default vbCancel

You can't trap the button as you can with MsgBox, but you can trap for
nothing in put (which Cancel does, even if data typed in)

ans = InputBox("Input a value")
If ans = "" Then Exit Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Jose" wrote in message
...
Hello,

I am using an input box and would like to exit the sub if
the user clicks cancel.

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default vbCancel

Hey Jose, just do this:
If ans = Cancel Then
finish
End If

Hope it works
Jorge
-----Original Message-----
Hello,

I am using an input box and would like to exit the sub if
the user clicks cancel.

Thanks
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default vbCancel

Sorry forgot the other part, finish is the name of the sub
sub finish ()
end
end sub
-----Original Message-----
Hello,

I am using an input box and would like to exit the sub if
the user clicks cancel.

Thanks
.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default vbCancel

Hi Jose,

If you use the Excel inputbox, you can trap the cancel.
For example:

Sub DemoInputbox()
Dim res As Variant

res = Application.InputBox("What is your name?")
If res = False Then
MsgBox "You cancelled!"
Exit Sub
ElseIf res = "" Then
MsgBox "You failed make an entry!"
Else
MsgBox "You entered " & res
End If

End Sub

---
Regards,
Norman

Jose" wrote in message
...
Hello,

I am using an input box and would like to exit the sub if
the user clicks cancel.

Thanks





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default vbCancel

Jose,

Try something like

Dim S As String
S = InputBox("Enter something")
If StrPtr(S) = 0 Then
Exit Sub
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Jose" wrote in message
...
Hello,

I am using an input box and would like to exit the sub if
the user clicks cancel.

Thanks



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default vbCancel

I"m looking for answer to same question except my input returns an integer answer so when I try to use this (or the other suggestions to this post) I get a type mismatch errro.
Any help is always appreciated

"Chip Pearson" wrote:

Jose,

Try something like

Dim S As String
S = InputBox("Enter something")
If StrPtr(S) = 0 Then
Exit Sub
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Jose" wrote in message
...
Hello,

I am using an input box and would like to exit the sub if
the user clicks cancel.

Thanks




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default vbCancel

An inputbox (as opposed to a message box) returns value "" if Cancel is
pressed. So the code below should work with an inputbox

Sub InputTest()
Dim Answer As String
Answer = InputBox("Enter value")
If Answer = "" Then
MsgBox ("Cancel pressed")
Exit Sub
Else
MsgBox ("Value entered: " & Answer)
End If
End Sub

For a messagebox the vbCancel does work:
if msgbox("Hi there",vbOKCancel) = vbCancel then msgbox("Cancel pressed")


"jeffP" wrote in message
...
I"m looking for answer to same question except my input returns an integer

answer so when I try to use this (or the other suggestions to this post) I
get a type mismatch errro.
Any help is always appreciated

"Chip Pearson" wrote:

Jose,

Try something like

Dim S As String
S = InputBox("Enter something")
If StrPtr(S) = 0 Then
Exit Sub
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Jose" wrote in message
...
Hello,

I am using an input box and would like to exit the sub if
the user clicks cancel.

Thanks






  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 56
Default vbCancel

Hi JeffP
In addition to Binzelli's answer:
also consider using Application.InputBox which restricts the type of input
expected:
MyNumber = Application.InputBox("Give a number please", "Number ?", , , , ,
, 1) '1 = Number see help for other types
If MyNumber < False Then
MsgBox "The number is " & MyNumber
Else: MsgBox "Cancelled was pressed", vbInformation
End If

HTH
Cordially
Pascal

"jeffP" a écrit dans le message de
...
I"m looking for answer to same question except my input returns an integer

answer so when I try to use this (or the other suggestions to this post) I
get a type mismatch errro.
Any help is always appreciated

"Chip Pearson" wrote:

Jose,

Try something like

Dim S As String
S = InputBox("Enter something")
If StrPtr(S) = 0 Then
Exit Sub
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Jose" wrote in message
...
Hello,

I am using an input box and would like to exit the sub if
the user clicks cancel.

Thanks






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
EXIT SUB UPON VBCANCEL FARAZ QURESHI Excel Discussion (Misc queries) 2 April 9th 09 12:38 PM
What type is vbCancel? Shatin Excel Programming 5 February 18th 04 05:10 PM
vbCancel Robin Clay[_3_] Excel Programming 6 October 24th 03 12:03 PM


All times are GMT +1. The time now is 11:13 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"