+ Reply to Thread
Results 1 to 10 of 10

Double up msg box

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2012
    Location
    molde
    MS-Off Ver
    Excel 2010
    Posts
    49

    Double up msg box

    Hi
    I'm trying to run this Sub before closing my workbook. If this is the only workbook open, I want to quit excel, but if there's more workbooks open, I just want to close this one. My problem is that the messagebox appear two times. Does enyone know how to fix this? I tried to put in the enableevents property, but I don't even know if I'm on the right track here...

    Sub Auto_Close()
    
    
    
    
    
     Dim answer As Integer
    
     answer = MsgBox("Alle endringer du har gjort vil bli lagret. Der som du ikke ønsker dette, må de omgjøres før du avslutter. Vil du fortsatt avslutte?", vbYesNo + vbQuestion, "VIKTIG")
    
     If answer = vbYes Then
    
     Application.ScreenUpdating = False
    
     Worksheets("Brukerlogg").Activate
    
     ActiveSheet.Unprotect Password:="password"
    
     Range("F10").Value = Now()
    
     ActiveSheet.Protect Password:="password"
    
     Application.DisplayFullScreen = False
    
     Application.ScreenUpdating = True
    
     ThisWorkbook.Save
    
    
    
     Application.EnableEvents = False
    
     If Worksheets.Count < 1 Then
     ThisWorkbook.Close
     Else
     Application.Quit
     End If
     Application.EnableEvents = True
    
     Else
     Cancel = True 
    End If
     
    End Sub
    Last edited by c00ly81; 01-27-2015 at 09:21 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Double up msg box

    Hi and welcome to the forum. Unfortunately your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    10-01-2012
    Location
    molde
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Double up msg box

    Think it's OK now. Still hoping for responses!

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Double up msg box

    c00ly81,

    2 things. Your code has an if statements that checks if there is more than one sheet... should it not be one open workbook?
    Second, if you go to the vba editor, and double click on the "ThisWorkbook" on the list, you can paste the code below, and remove Auto_Close macro. By using the beforeclose event of the workbook you'll avoid the double-triggering of the Auto_Close macro.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
    Dim answer As Integer
    
    answer = MsgBox("Alle endringer du har gjort vil bli lagret. Der som du ikke ønsker dette, må de omgjøres før du avslutter. Vil du fortsatt avslutte?", vbYesNo + vbQuestion, "VIKTIG")
    
    If answer = vbYes Then
    
    Application.ScreenUpdating = False
    
    Worksheets("Brukerlogg").Activate
    
    ActiveSheet.Unprotect Password:="password"
    
    Range("F10").Value = Now()
    
    ActiveSheet.Protect Password:="password"
    
    Application.DisplayFullScreen = False
    
    Application.ScreenUpdating = True
    
    ThisWorkbook.Save
    
    
    
    Application.EnableEvents = False
    
    If Worksheets.Count < 1 Then  'Should this be Workbooks.Count?
    ThisWorkbook.Close
    Else
    Application.Quit
    End If
    Application.EnableEvents = True
    
    Else
    Cancel = True
    End If
    
    End Sub
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  5. #5
    Registered User
    Join Date
    10-01-2012
    Location
    molde
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Double up msg box

    Re: Double up msg box



    Can't seem to edit, but besides workbook.count, should it be > 1 instead of <1?
    Arkadi: Thank you so much... Should have seen it myself, but this fixed everything! :D
    Last edited by c00ly81; 01-28-2015 at 07:26 AM.

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Double up msg box

    Can't seem to edit, but besides workbook.count, should it be > 1 instead of <1?

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Double up msg box

    Hi,

    Is this AutoRun macro called from the Workbook Close event?

    It comes up twice because of the first line after the DIM statement, and then presumably again when the workbook is closed later in the macro. Not sure why the enable events isn't preventing that.

    But 2 questions.
    1. If it's fired by the Workbook close event why do you need the Thosworkbook.close instruction? Why not just let autorun and retrurn control back to the Workbook Close event so that is closes naturally? Which of course raises the next question..

    2. Why do you need an autorun macro. Why not just put the relevant code in the Workbook_Close event in the first place?

  8. #8
    Registered User
    Join Date
    10-01-2012
    Location
    molde
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Double up msg box

    Hi Arkadi and thanks for quick response!

    Maby I should have been more presice, but this code is also connected to a "Close workbook" button. So if i put it in to "This workbook" I can't use it with the button. Isn't that correct? I'm new to this, so maby I ask a bit stupid questions... :P

    I have to ask if there's more workbooks open, cause I don't want to quit excel if other workbooks are open. I only want to quit the workbook containing this code. But if this is the only workbook open, I don't want excel to stay open blank, with only the gray background. If you know what I meen.

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Double up msg box

    Try this:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Dim answer: answer = MsgBox("Alle endringer du har gjort vil bli lagret. Der som du ikke ønsker dette, må de omgjøres før du avslutter. Vil du fortsatt avslutte?", vbYesNo + vbQuestion, "VIKTIG")
       If answer = vbYes Then
        Application.ScreenUpdating = False
        Worksheets("Brukerlogg").Activate
        ActiveSheet.Unprotect Password:="password"
        Range("F10").Value = Now()
        ActiveSheet.Protect Password:="password"
        Application.DisplayFullScreen = False
        Application.ScreenUpdating = True
        ThisWorkbook.Save
        Application.EnableEvents = False
        If Worksheets.Count < 1 Then ThisWorkbook.Close Else Application.Quit
       Application.EnableEvents = True
      Else
       Cancel = True
      End If
    End Sub
    Last edited by abousetta; 01-27-2015 at 09:54 AM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  10. #10
    Registered User
    Join Date
    10-01-2012
    Location
    molde
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Double up msg box

    Thanx! I'll try it first thing tomorrow! I'll keep you updated.

+ 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. [SOLVED] Before double click event with if statements (Double clicking blank cell stops bdc firing)
    By camdenpars in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-27-2014, 09:03 AM
  2. Changing cell properties with double click, then revert with another double click
    By mweber2525 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-01-2014, 01:40 PM
  3. Copy double-clicked cell to next empty row in different sheet with each double-click
    By erhathaway in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-25-2013, 12:06 AM
  4. Double stack and double line graph
    By Karen311261 in forum Excel General
    Replies: 5
    Last Post: 10-06-2010, 11:58 AM
  5. Double Locks ? Double Trouble
    By JXBlack in forum Excel General
    Replies: 1
    Last Post: 10-04-2009, 03:33 AM

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