Thanks to Siddharth Rout at this Post I learned how to save a sheet to a new Worksheet. Now my question is how I can add Date and Time of file creation like:
Change
FName = "C:\Users\somebody\Documents\TestSheet" & _
Format(Range("E19"), "mmm-d-yyyy") & ".xlsm"
to
FName = "C:\Users\somebody\Documents\TestSheet_" & _
Format(Date, "ddmmmyyyy") & ".xlsm"
If you are picking the date from Range("E19")
then ensure that the cell has a valid date.. In such a case the code becomes
FName = "C:\Users\somebody\Documents\TestSheet_" & _
Format(Range("E19"), "ddmmmyyyy") & ".xlsm"
To complete Siddharth's solution, here is the code to also include the time in the file name:
Function SaveDateTime() as String
Dim SaveTime As Integer
SaveTime = Round(Timer / 3600, 0)
Dim AMPM As String: AMPM = "AM"
If SaveTime >= 12 Then
AMPM = "PM"
If SaveTime > 12 Then
SaveTime = SaveTime - 12
End If
End If
SaveDateTime = "C:\Users\somebody\Documents\TestSheet_" & _
Format(Date, "ddmmmyyyy") & "_" & _
SaveTime & AMPM & ".xlsm"
End Function
Note that you could change Round()
by Int()
if you want the time to round down instead of just round. And also, pay attention on the language settings on the PC you will run this on because the date format depends on it.
Edited: Even simpler solution
Function SaveDateTime() as String
SaveDateTime = "C:\Users\somebody\Documents\TestSheet_" & _
Format(Date, "ddmmmyyyy") & "_" & _
Format(Time, "hAM/PM") & ".xlsm"
End Function
I have the following working well but would like to reverse the order. File name first, date and time second. So far have not figured out a way.
Sub SaveToLocations()
' Saves active file to current plus two other backup locations, appends system date and time in front of file name in backup locations.
Dim datim As String
datim = Format(CStr(Now), "yyyy_mm_dd_hh_mm_ss_")
ActiveWorkbook.SaveCopyAs "I:\FilesBackup\" & datim & ActiveWorkbook.Name
ActiveWorkbook.SaveCopyAs "E:\CS Docs\FilesBackupCS\" & datim & ActiveWorkbook.Name
ActiveWorkbook.Save
End Sub