Solved

How to code to stop saving Excel workbook in "Workbook_BeforeSave"

Posted on 2014-01-22
3
280 Views
Last Modified: 2014-01-22
I added some codes inside "Workbook_BeforeSave" to check something.  If not meeting my requirements, I used "Exit Sub".  But the workbook was still saved.  Is anyway I can stop saving process?
0
Comment
Question by:jjxia2001
  • 2
3 Comments
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39800116
Put

Cancel = True

in the code at the point you know something's wrong.
0
 

Author Comment

by:jjxia2001
ID: 39800167
I added it in and didn't work.  See my code below.


Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    On Error GoTo Errorhandler
   
               
        If Range("AccountNumber") = "" Then
        MsgBox ("Please enter SAI Number")
        Range("AccountNumber").Select
        Cancel = True
        Exit Sub
        End If
   
        If Range("PolicyEffectiveDate") = "" Then
        MsgBox ("Please enter Policy Effective Date")
        Range("PolicyEffectiveDate").Select
        Cancel = True
        Exit Sub
        End If
       
        Dim bln As Boolean
        bln = UserHasAccess()
       
        If bln Then
       
            Call getUserID
   
            frmWaitBox.Show modal
            subRemoveCloseButton frmWaitBox
            frmWaitBox.Repaint

            Call Driver
       
            Unload frmWaitBox
                   
            Exit Sub
        Else
            MsgBox ("Archiving of responses failed - possibly because you are offline.  Your Knowledge Edge results are still valid.")
            Exit Sub
        End If
   

Errorhandler:
    MsgBox ("Archiving of responses failed - possibly because you are offline.  Your Knowledge Edge results are still valid.")
 
    lngMessResult = SendErrorEmail("Error" + " contains one or more invalid range references")
 
End Sub
0
 
LVL 46

Assisted Solution

by:Martin Liss
Martin Liss earned 500 total points
ID: 39800230
There's probably something wrong with your range tests because this works for me.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Const TEST = 5

If TEST = 5 Then
    Cancel = True
    MsgBox "You can't close me"
    Exit Sub
End If

End Sub

Open in new window


To find out, put a breakpoint on the If Range("AccountNumber") = "" Then line and when the code gets there, press F8 repeatedly to trace the code execution line by line.

If you need it here's an article I wrote on how o use the Debugger.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question