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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro Cancel Button

Status
Not open for further replies.

Kaos2800

Programmer
Jan 16, 2001
17
US
Question, how do I set it up so that when I click the cancel it exits the macro without any errors or running any more of the VBA code?
 
Hi,
Are you trying to get out of a UserForm? You probably have a Cancel CommandButton and the Window X.

If you are talking about the Cancel CommandButton, there is a Click event for that button. In the VBE, double click the button and view the code. At this point, you would run any cleanup routine and finally

Unload Me

Hope this helps :) Skip,
metzgsk@voughtaircraft.com
 
I'm using the standard Message box. Here is the code I have.

Public Sub Timestamp()

'Init
Dim time As String
Dim Hours As String
Dim Min As String
Dim AMPM As String
Dim TestPeriod As String

time = ""
AMPM = "AM"

Title = "Enter the time"
Message = "Example, 1:30 AM = 1.30.0, 4:56 PM = 4.56.1 Click OK to exit."
Default0 = 0
time = InputBox(Message, Title, Default0)
If time = "0" Then GoTo Exitloop

TestPeriod = Mid(time, 2, 1)

'Tests for double digits
If TestPeriod <> &quot;.&quot; Then
Hours = Left(time, 2)
Min = Mid(time, 4, 2)
If Hours = &quot;12&quot; Then AMPM = &quot;PM&quot;
GoTo Finish
End If

'For the rest of the times
Hours = Left(time, 1)
Min = Mid(time, 3, 2)
If Right(time, 1) = &quot;1&quot; Then AMPM = &quot;PM&quot;
GoTo Finish

Finish:
Selection.Value = Hours & &quot;:&quot; & Min & &quot; &quot; & AMPM
ActiveCell.Offset(1, 0).Select
Call Timestamp

Exitloop:
End Sub

Here is the code for it. The problem is when I click on cancel, it runs Finish. I've tried adding an If statment at the begining of finish, but the program ignores it.

I'm the only one that uses it, so I'm not worried about it being pretty or foul proof. But I do click on cancel every now and again instead of hitting the enter key, or clicking on OK.
 
Hi,
You want to check the return value from the msgbox.

If MsgBoxAnswer = xlCancel Then
' do your exit routin
Exit Sub
Else
....

End if

Skip,
metzgsk@voughtaircraft.com
 
I had to change it to just cancel but that worked, Thanks!
 
I am sorry, it should have been vbCancel

:-( Skip,
metzgsk@voughtaircraft.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top