Tuesday, September 2, 2014

VBA saves as filename plus add date to filename

Process to save a file with a specific name and add the date to the filename


Dim strDataPath As String

strDataPath = "C:\YOURLOCATION"

ActiveWorkbook.SaveAs FileName:=strDataPath & "CI Services Workstack Automation Data" & "-" & Format$(Date, "DD-MM-YY") & ".xls"

' ~~~ strDataPath is your location where the file is to be saved

Process to save the current filename, and, add the date to filename

Sub MyFileSaveAs()
' File needs to be saved as a xlsm
            Dim last_dot As Long
           last_dot = InStrRev(ActiveWorkbook.FullName, ".")
           Dim Name As String

Name = Left$(ActiveWorkbook.FullName, last_dot - 1) & "-" & Format$(Date, "DD-MM-YY") & Mid$(ActiveWorkbook.FullName, last_dot)

        ActiveWorkbook.SaveAs Filename:=Name, FileFormat _
    :=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub

No comments:

Post a Comment