Excel 2013 VBA - Macro to save a workbook as today's date and time

How can I write a sub so that it saves the current workbook as today's date and current time.
just like this:
2017-12-13 -- 2100.xlsm means that the file was saved on December 13, 2017 9:00 pm

This sub is not working.
What is wrong with it?
Can someone create the above code?

Sub FilenameDateTime()
ActiveWorkbook.SaveAs Format(Now(), "yyyy-m-d  h\.mm.xlsm"), FileFormat:=52
End Sub
brothertruffle880Asked:
Who is Participating?
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Try it like this...

Sub FilenameDateTime()
ActiveWorkbook.SaveAs Format(Now(), "yyyy-m-d  hhmm"), FileFormat:=52
End Sub

Open in new window


Since you are not providing the path/location, if ActiveWorkbook is the new workbook created during runtime, it will be saved at the default temp folder.

Or if you want to save the activeworkbook in the same location where the macro workbook is saved, try this...

Sub FilenameDateTime()
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Now(), "yyyy-m-d  hhmm"), FileFormat:=52
End Sub

Open in new window

0
 
brothertruffle880Author Commented:
Thanks!  PERFECT!
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.