Home > Back-end >  Adding the current timestamp to the end of file name [duplicate]
Adding the current timestamp to the end of file name [duplicate]

Time:09-16

Background of current VBA code

At the moment I have the following VBA code, which will select a range of cells from a excel workbook sheet and create a csv of these cells in the directory shown below, where the csv filename is currently "PersonalFinances"

Sub CreateNewCSV2()

  Range("A4:AK32").Select
  Selection.Copy
  Workbooks.Add
  ActiveSheet.Paste
  Application.CutCopyMode = False

    ActiveWorkbook.SaveAs Filename:= _
        "C:\Project Files\Personal\PersonalFinances.csv", FileFormat:= _
        xlCSV, CreateBackup:=False
        
End Sub

My Question: How can I modify my above code, so that each day that I run this macro to create the "PersonalFinances" CSV, that I can add the current days timestamp to the end of the CSV filename. So for example, if I ran the macro today, the csv filename would be: "PersonalFinances20210916.csv". Any help would be greatly appreciated.

CodePudding user response:

Just update as depicted below:

ActiveWorkbook.SaveAs Filename:= _
        "C:\Project Files\Personal\PersonalFinances" & Format$(Now, "yyyyMMdd") & ".csv",, FileFormat:= _
        xlCSV, CreateBackup:=False
  • Related