Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stopping an excel macro 1

Status
Not open for further replies.

4281967

MIS
Oct 14, 2005
74
US
I have a macro that uses an input box to get some data from the user. I used a OK/cancel button - what is the code to kill the macro if the user selects cancle?
thanks
 
something like:
Code:
Sub test()
myReply = msgbox("Text", vbOKCancel, "Title")
If myReply = vbCancel Then GoTo TheyPressedCancel
'...
'Your Code
'...
TheyPressedCancel:
End Sub
Obviously you can change the variables from myReply and TheyPressedCancel to whatever you want.

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
thanks - that was exactly what I needed - have a star!
 
[cheers]

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
I did make a few changes -
Since I am using an input box to capture a string of data, I ended up coding the cancel button like this:
Code:
    Dim inp
        
    inp = Application.InputBox("Input MM_DD" , "Input Required")
    
' Check to see if Cancel was pressed.
    If inp <> False Then

' MAIN CODE HERE
   
' END OF MAIN CODE
  
    End If
Exit Sub
Note: inp was originally dimensioned as a string, however it must be a var now because if the "If inp <> False Then"

- Hope this helps someone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top