Results 1 to 4 of 4
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Prior To Save (XP)

    Upon prior to saving a file I have code that performs a validation test. If certain validation points are in error then a UserForm is displayed with a validation warning message. Upon clicking the "Red X" of UserForm or deactivating the form I would like the code to stop and not continue saving the file.

    Example of Code:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Range("Validation") = 0 Then
    DisplayUserForm2
    End If
    Exit Sub


    Private Sub UserForm_Deactivate()
    End 'I have tried using Exit Sub but that did not work either.
    End Sub


    My challenge is the code continues to save the file anyway.

    Your assistance is appreciated.
    John

  2. #2
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Prior To Save (XP)

    After a little trial and error I came up with this resolution.


    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Range("Validation") = 0 Then
    DisplayUserForm2
    Cancel = True
    Else
    Cancel = False
    End Sub

    Regards,
    John

  3. #3
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Prior To Save (XP)

    Just to elaborate a little:

    Anytime you have code to do something BEFORE an event: (BeforeSave, BeforeClose, BeforePrint, BeforeDoubleClick, BeforeRightClick) the code is run, as the event indicates before that event is triggered and then the event is triggered. The code you add does not replace the event procedure.

    To run the code and prevent the event from triggering, you must set "Cancel = True" (as you did in your code). The default (run if not listed) is "Cancel = False"

    Steve

  4. #4
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Prior To Save (XP)

    Steve,

    Thanks for your insight on the "cancel' event.

    Regards,
    John

Posting Permissions

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