Message box appears twice using VBA Case

  • VBA Case on workbook close that includes massagebox that appears twice, why does it do this? (First time use hop this is right approach :) )

    Private Sub Workbook_BeforeClose(Cancel As Boolean)


    'Step 1: Activate the message box and start the check

    Select Case MsgBox("Reset Parameters?", vbYesNo)


    'Step 2: No button pressed, close

    Case Is = vbNo

    ActiveWorkbook.Save

    ActiveWorkbook.Close


    'Step 3: Yes button pressed, save the workbook and close

    Case Is = vbYes

    Application.Run "Update"

    ActiveWorkbook.Save


    'Step 4: Close your Select Case statement

    End Select


    End Sub

  • You need to add Event Enable = False elsewise your event BeforeClose macro will trigger again when it hits ActiveWorkbook.Close .

    Code
    ActiveWorkbook.Save 
    Application.EnableEvents = False      '<- added 
    ActiveWorkbook.Close

    Edited once, last by rollis13 ().

  • Many thanks for responding

    I have inserted the suggested line only to find that the code now does run and file simple closes with standard save prompt

    Did I interpret correctly or is there a better script?

    Private Sub Workbook_BeforeClose(Cancel As Boolean)


    'Step 1: Activate the message box and start the check

    Select Case MsgBox("Reset Parameters?", vbYesNo)


    'Step 2: No button pressed, close

    Case Is = vbNo

    ActiveWorkbook.Save

    Application.EnableEvents = False

    ActiveWorkbook.Close


    'Step 3: Yes button pressed, save the workbook and close

    Case Is = vbYes

    Application.Run "Update"

    ActiveWorkbook.Save


    'Step 4: Close your Select Case statement


    End Select

    End Sub

  • Yes, you did, not sure what I was doing but your remedy worked many thanks. :)


    Apologies for being such novice


    Many thanks for you help

  • Hi

    another quick novice question

    Is it possible, by msgbox, to ask a user if they wish to have a workbook saved to their filename (assuming they have done some work they wish to save as their own) and have the user enter their preferred new file name?


    I have developed an analysis model for a small hospital and want to have the model maintained with industry accepted parameters and not to have save overwrite the model setup and parameters.


    Hope this makes sense

    I'm developing code piece by piece due to my novice status :)

  • I would like to point out that this is a completely different problem than the title of this thread.

    In these cases, to have even a minimum of visibility in the Forum, I suggest starting a new discussion with a new appropriate title.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!