Closed Thread
Results 1 to 2 of 2

Workbook_BeforeSave macro not working

  1. #1
    Registered User
    Join Date
    02-27-2008
    Posts
    44

    Workbook_BeforeSave macro not working

    I am trying to force users to enter certain information in a spreadsheet, and if they don't enter it they won't be able to save the spreadsheet. e.g. if a bonus recommendation is over a certain threshold they have to enter an explanation. if they don't enter the explanation I want it to display a message and prevent them from saving the file.

    This is what I have at the moment...there are 3 different instances where I want the save to be prevented, but at the moment the messages display (I've entered test data) but the workbook still saves. any ideas what I'm doing wrong?

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    With Worksheets("Protected Data")
    If .Range("BL3").Value > 0 Then MsgBox "YOU MUST ENTER AN EXPLANATION FOR ANY RECOMMENDATION ABOVE THE GUIDELINES LIMITS"
    Cancel = True
    If .Range("BL3").Value = 0 Then Cancel = False

    If .Range("DP3").Value > 0 Then MsgBox "ONE-UP RATING MUST BE ENTERED FOR ALL EMPLOYEES WITH A RECOMMENDATION"
    Cancel = True
    If .Range("DP3").Value = 0 Then Cancel = False

    If .Range("DO927").Value > 0 Then MsgBox "YOU MUST ENTER A COUNTRY FOR ALL CONTRIBUTIONS YOU ARE MAKING TO OTHER BU'S"
    Cancel = True
    If .Range("DO927").Value = 0 Then Cancel = False
    End With
    End Sub

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Workbook_BeforeSave macro not working

    Please edit your post and put your VBA into CODE tags -- use the # icon.

    The problem you have is that for ex. BL3 value > 0 but DO927 is = 0 then the Cancel will be set to False, ie file saved.

    You should think about leaving the tests as and when a Cancel = True is established... ie

    Please Login or Register  to view this content.

Closed Thread

Thread Information

Users Browsing this Thread

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

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