Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-22
3
Medium Priority
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
Martin Liss earned 2000 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 49

Assisted Solution

by:Martin Liss
Martin Liss earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

715 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