Save files as date & time by default! This may be simple and I'm trying to figure out myself too..

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
When i save the WORKBOOK's name by default it save as BOOK1, but I want it to be today's (=now) date and time instead on Book1...Is that possible...

I check the options the can't seems to locate it:biggrin:


Please help thanks!
Ped:)
 
Last edited:
I don't think that is possible as a default. For an individual workbook you can use code like the following in the ThisWorkbook module

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
    Cancel = True
    Application.EnableEvents = False
    ActiveWorkbook.SaveAs Filename = Format(Now, "dd-mm-yyyy hh-mm-ss")
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
I suppose you could also put Peter's code in a general module (change it to a standard sub as well) and add that to Personal.xls. That way you can invoke it in any workbook.
 
Upvote 0
I don't think that is possible as a default. For an individual workbook you can use code like the following in the ThisWorkbook module

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
    Cancel = True
    Application.EnableEvents = False
    ActiveWorkbook.SaveAs Filename = Format(Now, "dd-mm-yyyy hh-mm-ss")
    Application.EnableEvents = True
End If
End Sub


I was not able to get back yesterday due to blackOUT:::)

So here I am again. VoG, thanks alot for helping...but when try to save the workbook it gives error message...
..on ho..now it is not giving error message any more but when i close it gives me the option to "SAVE AS" so I have to write something as name of the workbook and then it does not save by DATE & time...


Please advice
 
Last edited:
Upvote 0
Sorry - small error

Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
    Cancel = True
    Application.EnableEvents = False
    ActiveWorkbook.SaveAs Filename:=Format(Now, "dd-mm-yyyy hh-mm-ss")
    Application.EnableEvents = True
End If
End Sub

This requires the workbook to be already saved or you will be prompted for the file name. To avoid that

Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = True
    Application.EnableEvents = False
    ActiveWorkbook.SaveAs Filename:=Format(Now, "dd-mm-yyyy hh-mm-ss")
    Application.EnableEvents = True
End Sub

BUT that would prevent you from saving with a filename other than the current date and time :warning:
 
Upvote 0
Everytime I close existing workbook with this code in it..it is saving file as "22-08-2010 21-09-20" name that is todays date and that is very close (I will even be contant with this) thought ut does not cantain time in it.

VoG, please...if you have time..I am now thinking of creating a templete for my default workBook, and the current code create a new file and save it with current date as name...is it possible not to create new file and just save existing file with current date and time (Otherwise it will create a problem when I create a new workbook in the same day)???

Thank you so much for giving me a BIG helping hand...;)

pedie
 
Upvote 0
The time is this part 21-09-20 in hh-mm-ss.

I did it that way because you cannot have : in a filename. You could replace - with another valid character here:

Rich (BB code):
ActiveWorkbook.SaveAs Filename:=Format(Now, "dd-mm-yyyy hh-mm-ss")
 
Upvote 0
Sorry VoG, the file name I thought it was just date but no "22-08-2010 21-08-18" the second part of it is time thought it looks like date...:biggrin:

when i try the code below is giving error (new book before saving it i placed the code and when i close it is giving error)..the other code creates new file and name it date & time..
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
    Cancel = True
    Application.EnableEvents = False
    ActiveWorkbook.SaveAs Filename:=Format(Now, "dd-mm-yyyy hh-mm-ss")
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,867
Messages
6,193,427
Members
453,799
Latest member
shanley ducker

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