Solved

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

Posted on 2014-01-22
3
276 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 45

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 45

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now