Reminder when Saved

Raychill Canuck

Board Regular
Joined
Jan 4, 2006
Messages
57
So, I have some code (see below) that won't allow users to save the spreadsheet until certain cells are filled in. I now want to include code that will remind the user to save as macro enabled spreadsheet (xlsm), but only if the file isn't already xlsm.

Here's my code so far:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.CountA(Range("P19, P29, P31")) <> _
    Range("P19, P29, P31").Count Then
    MsgBox "Time To Reoccupation, Quote Done By and Job Type must be filled in before saving quote"
    Cancel = True
End If
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
If you are putting this VBA code in the workbook, aren't you already saving it as an "xlsm" (so by the time the users get it, it is already an "xlsm")?
How would it be anything other than an "xlsm" by the time your users get it?
 
Upvote 0
So I tested the code suggested in the link, and it works.

However I can't figure out how to combine it with my original code so that the user can't save until the required fields are filled in. Any suggestions?
 
Upvote 0
Just try placing the block of code under the other (in the same procedure).
 
Upvote 0
So I tried this. When I go to save it brings up the error message that the fields are not filled in, but instead of stopping the save until the fields are filled out, it proceeds with the save (as xlsm).

My code is as follows:
Code:
Option Explicit

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
 If Application.CountA(Range("P19, P29, P31")) <> _
    Range("P19, P29, P31").Count Then
    MsgBox "Time To Reoccupation, Quote Done By and Job Type must be filled in before saving quote"
    Cancel = True
End If
Dim fname As Variant
    On Error GoTo ErrorHandler
    If SaveAsUI Then
        Cancel = True   'Cancel the original SaveAs
         'Get filename (with path) for saving
        fname = Application.GetSaveAsFilename(fileFilter:="Excel Marcro-Enabled Workbook (*.xlsm),*.xlsm")
        If fname = False Then Exit Sub  'Exit if user hit Cancel
        Application.EnableEvents = False  'Prevent this event from firing
        ThisWorkbook.SaveAs Filename:=fname, FileFormat:=52
          '52 = xlOpenXMLWorkbookMacroEnabled = xlsm (with macro's in 2007-2010)
        Application.EnableEvents = True  'Re-enable events
    End If
Exit Sub
ErrorHandler:
    Application.EnableEvents = True   'So events are never left disabled.
    MsgBox "An error occured during save." & Err.Number, vbCritical, "Error"

End Sub
 
Upvote 0
OK. That is because the second part is running no matter what happens in the first part. So I think what we want to do is change the first "END IF" to an "ELSE", and add the "END IF" to the end, like this:
Code:
Option Explicit


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
 
If Application.CountA(Range("P19, P29, P31")) <> _
    Range("P19, P29, P31").Count Then
    MsgBox "Time To Reoccupation, Quote Done By and Job Type must be filled in before saving quote"
    Cancel = True
Else
    Dim fname As Variant
    On Error GoTo ErrorHandler
    If SaveAsUI Then
        Cancel = True   'Cancel the original SaveAs
         'Get filename (with path) for saving
        fname = Application.GetSaveAsFilename(fileFilter:="Excel Marcro-Enabled Workbook (*.xlsm),*.xlsm")
        If fname = False Then Exit Sub  'Exit if user hit Cancel
        Application.EnableEvents = False  'Prevent this event from firing
        ThisWorkbook.SaveAs Filename:=fname, FileFormat:=52
          '52 = xlOpenXMLWorkbookMacroEnabled = xlsm (with macro's in 2007-2010)
        Application.EnableEvents = True  'Re-enable events
    End If
End If

Exit Sub
ErrorHandler:
    Application.EnableEvents = True   'So events are never left disabled.
    MsgBox "An error occured during save." & Err.Number, vbCritical, "Error"

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top