PDA

View Full Version : Solved: Backing Up in a Macro



kisinana
07-12-2008, 11:42 PM
If you make the wrong selection in a message box, how do make the cancel button in the input box or the small x in the top right corner return you to the message box?

Simon Lloyd
07-13-2008, 12:49 AM
If its a message box and you are giving a choice i.e YES, NO, CANCEL, how can you possibly say it's a wrong selection? surely its the preference of the user as to which one they click, if you want to force the user to make only one choice then only have the message box as vbOkCancel and handle it like this:

Sub Test()
Dim Mbox As Long
Nxt:
Mbox = MsgBox("Cancel or X", vbOKCancel)
If Mbox= vbCancel Then MsgBox GoTo Nxt
End If
End Sub

mdmackillop
07-13-2008, 01:56 AM
I would code it to avoid the possibility of error.

Sub Test()
Dim Mbox As Long
Mbox = MsgBox("Please choose", vbYesNoCancel)
Select Case Mbox
Case vbYes, vbNo, vbCancel
MsgBox "Thank you for choosing this option"
Case Else
'There is none
End Select
End Sub

Bob Phillips
07-13-2008, 02:59 AM
I would have thought he means something like



Sub Test()
Dim ans As VbMsgBoxResult
Do

ans = MsgBox("Please choose", vbYesNoCancel)
If ans = vbYes Then

'the code for yes option
End If
Loop Until ans = vbYes Or ans = vbCancel
End Sub

Simon Lloyd
07-13-2008, 07:57 AM
Ok, i admit there was a typo in my code but i believe that this does exactly the same as yours Bob only shorter!

Sub Test()
Dim Mbox As Long
Nxt:
Mbox = MsgBox("Cancel or X", vbOKCancel)
If Mbox = vbCancel Then GoTo Nxt
End Sub

kisinana
07-13-2008, 08:08 AM
Sorry my question was not giving enough information.
My message box asks if secondary information is needed. Choices are yes or no. If no it just carries on and completes the calculation. If yes it brings in an input box and asks for a value.
I have been able to add a cancel button to the input box, but I need to have it, when chosen take you back to the message box. This is incase you made the wrong selection in the message box and there was no secondary value needed.
I hope this makes it clearer

Simon Lloyd
07-13-2008, 08:17 AM
Clicking cancel on an inputbox cant be handled like that as far as i know, however clicking cancel on an inputbox is the same as clicking OK when there is no value so:

Sub Test()
Dim Mbox As Long
Nxt:
Mbox = MsgBox("Cancel or X", vbYesNoCancel)
If Mbox = vbYes Then
If InputBox("click", "test box") = "" Then GoTo Nxt
End If
End Sub