+ Reply to Thread
Results 1 to 9 of 9

Saving contradictions in VBA

  1. #1
    Registered User
    Join Date
    07-10-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    18

    Saving contradictions in VBA

    Fellow Excellers,

    I'm having a hard time working out two codes that contradict eachother.
    Please take a look at it as I'm out of ideas:

    Code 1
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    'Close workbook without saving changes.
    Me.Saved = True
    End Sub

    Code 2
    Sub Clean_Save_Close()
    Application.Run "'All-in-one.xlsm'!ThisWorkbook.Clear_ZS_Calc"
    Application.Run "'All-in-one.xlsm'!ThisWorkbook.Clear_USD_Calc"
    Application.Run "'All-in-one.xlsm'!ThisWorkbook.Sort_Customer_Confirmationlist"
    Sheets("4.2").Select
    Range("E10").Select
    Selection.ClearContents
    Range("L10").Select
    ActiveCell.FormulaR1C1 = "ne"
    Sheets("0").Select
    ThisWorkbook.Close savechanges:=True
    End Sub

    Code 1 prevents the save prompt from showing up whenever you hit X.
    Code 2 should be saving the excel (this is set to a button).

    Somehow code 1 prevents code 2 from saving the sheet.
    Please help!

    Regards,
    MitchelDZ

  2. #2
    Forum Contributor
    Join Date
    05-04-2012
    Location
    Stamford,Connecticut,USA
    MS-Off Ver
    Excel 2003
    Posts
    105

    Re: Saving contradictions in VBA

    Replace Me.Saved=True for this:

    Application.DisplayAlerts = False

  3. #3
    Registered User
    Join Date
    07-10-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Saving contradictions in VBA

    I like the idea, but that doesn't stop the save prompt.

  4. #4
    Forum Contributor
    Join Date
    05-04-2012
    Location
    Stamford,Connecticut,USA
    MS-Off Ver
    Excel 2003
    Posts
    105

    Re: Saving contradictions in VBA

    How about this:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-10-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Saving contradictions in VBA

    Looking good. But it's not what I'm going for.

    I only want the workbook to save when I hit the button that triggers the 2nd code (the code also exits Excel).
    Any other way of exiting Excel should leave the workbook unsaved and not prompt the save box.
    Last edited by MitchelDZ; 07-10-2012 at 10:10 AM.

  6. #6
    Forum Contributor
    Join Date
    05-04-2012
    Location
    Stamford,Connecticut,USA
    MS-Off Ver
    Excel 2003
    Posts
    105

    Re: Saving contradictions in VBA

    So how about changing the Me for thisworkbook
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-10-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Saving contradictions in VBA

    This still presents the same problem: it won't let code 2 save the file.

    (I really appreciate you trying to help, don't give up on me yet)

  8. #8
    Forum Contributor
    Join Date
    05-04-2012
    Location
    Stamford,Connecticut,USA
    MS-Off Ver
    Excel 2003
    Posts
    105

    Re: Saving contradictions in VBA

    In your code 2 set the instruction in the opposite direction at the begining of the code.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    07-10-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Saving contradictions in VBA

    I got it! :D

    RaulRodriguez, thanks for helping me on my way.

    The solution:
    Since code 2 is closing my Excel anyhow I can turn off all private subs.
    The problem was that my private was running all the time even nanoseconds (whatever) before executing the line: ThisWorkbook.Close savechanges:=True

    The way it looks now:
    Code 1
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    'Close workbook without saving changes.
    ThisWorkbook.Saved = True
    End Sub

    Code 2
    Sub Clean_Save_Close()
    Application.EnableEvents = False ' <----- Key to the solution
    ThisWorkbook.Saved = False
    Application.Run "'All-in-one.xlsm'!ThisWorkbook.Clear_ZS_Calc"
    Application.Run "'All-in-one.xlsm'!ThisWorkbook.Clear_USD_Calc"
    Application.Run "'All-in-one.xlsm'!ThisWorkbook.Sort_Customer_Confirmationlist"
    Sheets("4.2").Select
    Range("E10").Select
    Selection.ClearContents
    Range("L10").Select
    ActiveCell.FormulaR1C1 = "ne"
    Sheets("0").Select
    ThisWorkbook.Close savechanges:=True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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