How to Add Date and Time To File Name Using VBA in Excel

Viewed 109994

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:

TestSheet_25May2013_5pm.xls

Sub SaveSheet()
    Dim FName As String

    ActiveSheet.Copy
    With ActiveSheet.UsedRange
        .Copy
        .PasteSpecial xlValues
        .PasteSpecial xlFormats
    End With
    Application.CutCopyMode = False


    FName = "C:\Users\somebody\Documents\TestSheet" & Format(Range("E19"), "mmm-d-yyyy") & ".xlsm"
    ActiveWorkbook.SaveAs Filename:=FName, _
                          FileFormat:=xlOpenXMLWorkbookMacroEnabled



End Sub

can you please let me know how to do this?

3 Answers

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
Related