Saving an Unsavable Workbook

Written by Allen Wyatt (last updated May 30, 2022)
This tip applies to Excel 2007, 2013, 2016, 2019, and Excel in Microsoft 365


6

Bill has a "before save" macro that tests three cells. If the cells are empty, then the macro stops the user from saving. In other words, they are required to fill in the cells before they can save the workbook. Bill would like to save this workbook as a template, but when he clears the three cells, the macro also stops him from saving as a template. He would like the template to be "clean," with a default of the three cells being empty. Users would create a new workbook based on the template and then (correctly) not be able to save unless the cells are filled in. Bill wonders how he can save the workbook as a template, with the three cells empty.

There are actually a number of different approaches you could take to save your template.

The first is to change the name of your workbook to something rather esoteric, such as X27TT3W.xlsm. Then, add some logic to your "before save" macro that checks to see if the name of the base workbook is X27TT3W. If it is, then allow the workbook (or template) to be saved regardless of the condition of the three cells. Once the template is saved, you can then rename it in Windows to your desired name, and it can be shared with your users. Unless they guess the esoteric name you chose, they won't be able to save the workbook without making sure the three cells are filled in.

Another approach is to simply add another event handler to your workbook—this one that executes when you first open the workbook—to clear the contents of the three cells. This could be something simple, like this:

Private Sub Workbook_Open()
    Sheet1.Cells(1,1).Clear
    Sheet1.Cells(2,1).Clear
    Sheet1.Cells(3,1).Clear
End Sub

This macro clears the cells at A1:A3. If you want to have it clear a different range, just change the three lines to reflect which cells you want to clear. Then, put something in the three cells (so that it passes your testing in the "before save" macro), and save it as a template.

Another approach that is very easy to implement is to simply disable events before you save the template. This is not done within a macro, but within the Immediate window in the Visual Basic Editor. All you need to do is enter this single line:

Application.EnableEvents = False

Immediately save your workbook as a template, and then enter the following line in the Immediate window of the Visual Basic Editor:

Application.EnableEvents = True

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13547) applies to Microsoft Excel 2007, 2013, 2016, 2019, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Cannot Add Words to Dictionary

We all run across words that are spelled correctly, but that Word isn't aware of. The solution is to add those words to ...

Discover More

Adding a Tool to Clear Borders

Excel allows you to easily add tools to the Quick Access Toolbar. If you want to add a tool that clears all the borders ...

Discover More

Store Common Macros in the Personal Macro Workbook

Want your macros to be available regardless of the workbook on which you are working? Here's how to store them in the ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Inserting Worksheet Values with a Macro

Macros are often used to process information in a worksheet. You may need your macro to change the values stored in ...

Discover More

Resetting Default Names for New Worksheets

When you add a new worksheet to a workbook, Excel gives it a default name that consists of "Sheet" followed by a number. ...

Discover More

Engineering Calculations

Need to normalize your data in some way so that all your values are in a given format? This tip presents a number of ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 3?

2018-07-09 12:36:29

Dave Bonin

In my case, I'm the only one writing macro code and templates for others to use.

To ensure other users do or don't do certain things, I built a simple function that
checks whether Application.UserName contains my name, "Bonin".

If it does, then I might have the code ask whether I'm saving the file as a "user" or
as a "developer".

If it does not, then I typically force the user option.

I include this function wherever I need to. For today's example, I would put it in a
Workbook_BeforeSave() macro.
___

Of course, if you have multiple developers, you can expand the Application.UserName
checks to include many users, some perhaps with different privileges.


2018-07-09 10:23:28

Steven

In the second scenario, that deletes the cell contents on open, wouldn't that then delete the valid contents entered by a user when they re-open the document? If true, then only a PDF printout of the spreadsheet would be viewable and be the only record of the valid contents, once the spreadsheet was opened the second time.


2018-07-09 09:08:50

Mathew

When writing complicated macros/code, I always includes a binary testing variable. I set it to true, while testing.
Then in the code itself, I use an If...then block to exclude running that section when Testvar = true.
When I'm done testing what I need, then I can reverse the variable in the if...then block, and work on another area.

This allows me to control what I want to run and what I want to isolate while testing.

At the end, I do have to go through the code and verify either: 1) all the testvar variable are correctly set, or 2) remove all the testing if...then blocks.


2018-07-07 15:45:16

Col Delane

The last option (disabling the Event Handler) can actually be done within a macro!
If you create one macro to disable events, viz;

Sub DisableEvents ()
Application.EnableEvents = False
End Sub

and another to enable them

Sub DisableEvents ()
Application.EnableEvents = True
End Sub

you can run these macros like any other without having to open the Visual Basic Editor.

You can either include them in the template workbook (best in a separate "Administrators" module, or add them to your Personal.xlsm so you can run them anytime for any workbook.


2018-07-07 08:31:18

ES

Something like this

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Ans As Integer
Dim Rng As Range

'List of cells
Const NotEmptyCells = "A1,A3,C13"
Set Rng = Range(NotEmptyCells)

'Format choice
Ans = MsgBox("Save As template ?", vbYesNoCancel + vbQuestion, "Save as... (format)")

'Stop default process
Application.EnableEvents = False

'
Select Case Ans
Case vbYes 'Template
Rng.ClearContents
ActiveWorkbook.SaveAs Filename:=Left(thisworbook.Name, InStr(".", ThisWorkbook.Name) - 1) & "_template.xltm", _
FileFormat:=xlOpenXMLTemplateMacroEnabled

Case vbNo 'All except template
If Application.CountA(Rng) = (Len(NotEmptyCells) - Len(Replace(NotEmptyCells, ",", ""))) Then
MsgBox "You must enter values into cells " & NotEmptyCells & Chr(13) & "FILE NOT SAVED.", vbOKOnly + vbExclamation, "ERRORS VALUES"
Else
'
ActiveWorkbook.Save
End If

Case vbCancel 'STOP

End Select

'Stop default process
Cancel = True

'Stop default process
Application.EnableEvents = True

'
Set Rng = Nothing
End Sub

ES


2018-07-07 07:15:16

Barry

Why not just have a special exit password in one of the cells or could even bee another cell way off the user screen e.g. AZ1000000. This password is an otherwise invalid value for the cell, which is tested in the "Before Save" macro which if present deletes the password and then saves the workbook.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.