+ Reply to Thread
Results 1 to 6 of 6

VBA workbook_BerforClose iteration

Hybrid View

  1. #1
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,408

    VBA workbook_BerforClose iteration

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
       Select Case MsgBox("Library Management System" & vbNewLine & vbNewLine & "Save and Close?", vbOKCancel)
       
    ' Cancel button pressed, so cancel the close
    
        Case Is = vbCancel
        Cancel = True
        
    ' OK button pressed, so save the workbook and close
    
        Case Is = vbOK
        ThisWorkbook.Close SaveChanges:=True
       
    'Close Select Case statement
    
        End Select
    
    End Sub
    This code re-enters the Before_Close routine when it executes the ThisWorkbookClose statement so the Msgbox is output again.

    Why ? And can this be avoided ?

  2. #2
    Valued Forum Contributor
    Join Date
    06-27-2010
    Location
    sYRIA
    MS-Off Ver
    Excel 2013
    Posts
    669

    Re: VBA workbook_BerforClose iteration

    Hi
    Try
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
        Select Case MsgBox("Library Management System" & vbNewLine & vbNewLine & "Save and Close?", vbOKCancel)
    
            ' Cancel button pressed, so cancel the close
    
        Case Is = 2
            Cancel = True
    
            ' OK button pressed, so save the workbook and close
    
        Case Is = 1
            Application.EnableEvents = False
            ThisWorkbook.Close SaveChanges:=True
    
            'Close Select Case statement
    
        End Select
        Application.EnableEvents = True
    End Sub

  3. #3
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: VBA workbook_BerforClose iteration

    All you need to do in the 2nd case is Me.Save. No need to use Application.EnableEvents.

  4. #4
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,408

    Re: VBA workbook_BerforClose iteration

    Both solutions work.

    Thank you for your help.

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: VBA workbook_BerforClose iteration

    Maybe :
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Static isSkip As Boolean
      If isSkip Then Exit Sub
    
      Select Case MsgBox("Library Management System" & vbNewLine & vbNewLine & "Save and Close?", vbOKCancel)
       
    ' Cancel button pressed, so cancel the close
    
        Case Is = vbCancel
        Cancel = True
        
    ' OK button pressed, so save the workbook and close
    
        Case Is = vbOK
        isSkip = True
        ThisWorkbook.Close SaveChanges:=True
    'Close Select Case statement
    
        End Select
    
    End Sub
    ThisWorkbook.Close method will raise Workbook_BeforeClose event, unless Application.EnableEvents is set to false (as in mohadin's code).
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  6. #6
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: VBA workbook_BerforClose iteration

    Reduced to essentials,

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      If MsgBox("Library Management System" & vbNewLine & vbNewLine & "Save and Close?", vbOKCancel) = vbOK Then
        Me.Save
      Else
        Cancel = True
      End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Run an entire loop instead of iteration by iteration when stepping into code?
    By Norcal1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2019, 04:20 PM
  2. Iteration within iteration
    By shamsul in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-04-2016, 07:09 PM
  3. [SOLVED] Iteration Help
    By devos in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-10-2014, 07:30 PM
  4. Iteration
    By StillLearning2 in forum Excel General
    Replies: 2
    Last Post: 02-10-2009, 02:06 PM
  5. [SOLVED] Need help with iteration
    By Ron M. in forum Excel General
    Replies: 7
    Last Post: 03-13-2006, 08:35 PM
  6. Iteration
    By M. Homayon in forum Excel General
    Replies: 1
    Last Post: 01-10-2006, 09:10 PM
  7. Iteration
    By Jan in forum Excel General
    Replies: 1
    Last Post: 01-10-2006, 07:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1