EXCEL MsgBox Confirmation of Macro Execution

Solved
laptitepero Posted messages 19 Status Member -  
 loloonthewave -
Hello!

Here's my question:
I have a macro that deletes rows, and I have put an execution button on my sheet.
(So far, so good.)

Now I would like to create a confirmation message to confirm executing the macro if someone clicks on this button.
While searching a bit, I found this macro:

Sub MyFunction()
If MsgBox("Irreversible operation. Do you wish to continue?", vbQuestion + vbYesNo, "QUESTION ...") = vbYes Then
...
...
...
Else
...
End If
...
End Sub

But I can't apply it to mine (the ..... don't help me).

Thank you for your help.

Laptitepero

5 answers

  1. Polux31 Posted messages 7219 Status Member 1 204
     
    Hello,

    The error is here:

    If vbCancel Then Exit Sub Else End If


    It should be:
    If yourmsgbox = vbCancel Then Exit Sub End If


    ;o)
    “What one conceives well is clearly expressed, and the words to say it come easily.”
    Nicolas Boileau
    4
    1. cavrom Posted messages 24 Registration date   Status Member Last intervention   3
       
      My God, it's so good when it works! A thousand times thank you!
      Have a nice day!
      0
  2. cavrom Posted messages 24 Registration date   Status Member Last intervention   3
     
    Hello everyone,
    I have exactly the same problem as lapetitepero, but your information is not helping me.
    My syntax is as follows:
     yourmsgbox = MsgBox("Are you sure you validate?", vbOKCancel, "confirmation") If vbCancel Then Exit Sub Else End If 


    Then my code to execute if you click on Ok is located here.
    In this case, whether I click on Ok or Cancel, it's the cancel case that executes. But where is the error?
    3
    1. cavrom Posted messages 24 Registration date   Status Member Last intervention   3
       
      For those interested, here is the response:
      'Msgbox Ok + Cancel yourmsgbox = MsgBox("Are you sure to validate?", vbOKCancel, "Confirmation") If yourmsgbox = vbCancel Then Exit Sub End If
      0
  3. Polux31 Posted messages 7219 Status Member 1 204
     
    Hello,

    The "little dots" are the code that must be executed, that is to say your code.

    Sub MyFunction() If MsgBox("Irreversible operation. Do you want to continue?", vbQuestion + vbYesNo, "QUESTION ...") = vbYes Then ' YOUR CODE IF THE ANSWER IS "YES" Else ' YOUR CODE IF THE ANSWER IS "NO" End If End Sub 


    ;o)
    --
    “What is well conceived is clearly said, And the words to say it come easily.”
    Nicolas Boileau
    1
    1. laptitepero Posted messages 19 Status Member
       
      Oki, and to say not to do anything if the answer is no,
      is there a special code?

      Also, for the part in () after MsgBox, what can I change?
      "QUESTION....", I put my question back "Irreversible operation...?"

      Thank you.
      0
    2. Polux31 Posted messages 7219 Status Member 1 204
       
      Well, there's no special code!!! If there's nothing to do, there's nothing to code... so you can remove the Else. Or you can put an Exit Sub in the Else to exit the procedure.

      In MsgBox, "QUESTION..." is the title that will appear in the blue bar of the dialog box. You can put whatever you want.
      0
    3. loloonthewave
       
      Great thank you, it's honestly perfect!!!!!
      0
  4. laptitepero Posted messages 19 Status Member
     
    If it can help you,
    here is my basic macro:

    Sub SuppLigne() Dim Lig As Long Sheets("Modèle").Select Lig = ActiveCell.Row Rows(Lig).Delete Sheets("Résultat").Rows(Lig).Delete Sheets("Constantes").Rows(Lig).Delete End Sub
    0
  5. laptitepero Posted messages 19 Status Member
     
    Yahoo, everything is working great!
    Thank you very much!
    0
    1. Polux31 Posted messages 7219 Status Member 1 204
       
      Content for you :o)

      Good luck and take care.

      ;o)
      0