Run a macro from a vbyesno Msg box response

B

Binngo

Hello
I am trying to create a msg box so that when the user clicks 'yes' it
triggers a macro to run. However at the moment when I click Yes the script
ends and no macro is run - the same happens for No and Cancel but that is
correct for those selections. The code I'm trying is shown below and sits at
the start of the marco in question. I'm new to this so have not sure what to
do. Any ideas?

Sub clean()
Msg = "Have you saved the current file data?"
Response = MsgBox(Msg, vbYesNoCancel)

If Response = vbYes Then
'<=== runactivemacro clean()
Exit Sub
End If

If Response = vbNo Then
MsgBox "Use the Save As function to Save the file before cleaning the sheet"
Exit Sub
End If

If Response = vbCancel Then
MsgBox "Action Cancelled"
Exit Sub
End If


' clean Macro
' Macro recorded etc etc then all the macro detail is here
 
B

Bob Phillips

This works fine for me

Sub Clean()
Dim Msg As String
Dim Response As Long

Msg = "Have you saved the current file data?"
Response = MsgBox(Msg, vbYesNoCancel)

If Response = vbYes Then
Call Clean2
ElseIf Response = vbNo Then
MsgBox "Use the Save As function to Save the file before cleaning
the sheet"
ElseIf Response = vbCancel Then
MsgBox "Action Cancelled"
End If

End Sub

Sub Clean2()
MsgBox "Clean2"
End Sub
 
R

RadarEye

Hi Binngo,

A other way to do could be:

Sub Clean()
Msg = "Have you saved the current file data?"
Select Case MsgBox(Msg, vbYesNoCancel)
Case vbYes
Call Clean2
Case vbNo
MsgBox "Use the Save As function to " & _
"Save the file before cleaning the sheet"
Case Else
MsgBox "Action Cancelled"
End Select

End Sub

Sub Clean2()
MsgBox "Clean2"
End Sub

HTH,

Wouter
 

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

Top