Often when creating a macro, we may need to know the save status a file we are working with. This post explores some of the options available for understanding and controlling this important aspect of file control.
Using the .saved property
Within the Visual Basic Editor we can view the Saved property of a workbook.
If you save a workbook it will change to True, but once you make a single change it will revert back to False.
Effectively, this property displays whether the current file has changed from the previously saved version.
Sub saveStatus() If ActiveWorkbook.Saved = False Then MsgBox "The file has changed since it was last saved" Else MsgBox "The file has not changed since it was last saved" End If End Sub
Even though it seems like this should be a read-only property, it is possible to change this it with VBA. This will trick Excel into thinking that the current file has or has not been changed.
It is this property which Excel uses to know whether to show the following message when closing the workbook.
There are two ways to avoid this message:
Method 1 – setting DisplayAlerts = False
Using the code below will not result in the “want to save your changes” message box appearing and the file will not be saved.
Application.DisplayAlerts = False ActiveWorkbook.Close Application.DisplayAlerts = True
Method 2 – use .saved = true
Just like method 1, using the code below will not result in the “want to save your changes” message box appearing and the file will not be saved.
ActiveWorkbook.Saved = True ActiveWorkbook.Close
Do you know the fastest way to learn foreign languages? It is to read, write, speak, and think in that language as often as possible. Apart from speaking, programming languages are no different. The more you immerse yourself in that language, the faster you will pick it up.
Therefore, what most people like you need is lots of examples that you can practice. That is why the 100 Excel VBA Macros eBook exists. It’s the book for all Excel users who want to learn how to read and write Excel macros, save time, and stand out from their peers. The book contains:
- 100 example codes to practice reading and writing macros that will embed the language into your thinking.
- An introduction to macros in Excel to ensure you can implement the VBA code in the book even if you have no prior knowledge.
- Consistent code layout between examples to enable you to understand the structure and easily customize the code to meet your needs.
- Downloadable workbook containing all the source code, so the examples can be added to your project to give you the benefit of VBA straight away.
Has a file ever been saved?
We have established what the .saved property can do. But, it is missing one a piece of information . . . it doesn’t tell us if the file has ever been saved.
If you create a new document in Excel it’s saved status displays as True. But once a change is made it to the workbook, it changes to False. So, the True/False status of the saved property does not help us to know if a file has ever been saved. But, we also have a number of options here.
Method 1 – Test the file path
A workbook which has never been saved will have a blank value for the File Path.
If ActiveWorkbook.Path = "" Then MsgBox "Not saved"
Method 2 – Use FILEDATETIME function
The FILEDATETIME function in VBA takes just one argument, a full File Path, and it returns the last time the file was modified.
If the file has been saved, a date and time will be returned to the Immediate Window, if not, an error will be returned.
By catching the error you will know if the file has ever been saved.
On Error Resume Next Debug.Print FileDateTime(ActiveWorkbook.Name) If Err.Number <> 0 Then MsgBox "The file has never been saved" End If On Error GoTo 0
Method 3 – Use BuiltinDocumentProperties
This approach is similar to the FILEDATETIME function.
Debug.Print ActiveWorkbook.BuiltinDocumentProperties("Last save time")
If the file has been saved a date and time will be returned to the Immediate Window, if not an error will be returned.
Just like the FILEDATETIME function you can catch the error if the file has never been saved.
When writing VBA code there are various options and settings we can use to understand and control how and if a file is saved. In a number of applications this is crucial to ensure the macro will run smoothly.
Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.
By entering your email address you agree to receive emails from Excel Off The Grid. We’ll respect your privacy and you can unsubscribe at any time.
If you’ve found this post useful, or if you have a better approach, then please leave a comment below.
Do you need help adapting this to your needs?
I’m guessing the examples in this post didn’t exactly meet your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site) you should be able to adapt it to your needs.
But, if you’re still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise. List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don’t go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts: