How to save an Excel filename with timestamp?

Have you ever tried to save an Excel file with current timestamp? This article will show you method to achieve it.

Save an Excel filename with timestamp with VBA code


arrow blue right bubble Save an Excel filename with timestamp with VBA code


You can run the below VBA code to save an Excel filename with timestamp. Please do as follows.

1. In the workbook you need to name it by current timestamp, please press the Alt + F11 keys simultaneously to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, please click Insert > Module. Then copy the below VBA code into the Code window. See screenshot:

VBA code: Save an Excel filename with timestamp

Sub SaveAsFilenameWithTimestamp()
'Updated by Extendoffice 20191223
Dim xWb As Workbook
Dim xStrDate As String
Dim xFileName As Variant
Dim xFileDlg As FileDialog
Dim i As Variant
Application.DisplayAlerts = False
Set xWb = ActiveWorkbook
xStrDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
If Right(xWb.Name, 4) = "xlsm" Then
  xFileName = Application.GetSaveAsFilename(xStrDate, "Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
Else
  xFileName = Application.GetSaveAsFilename(xStrDate, "Excel Workbook (*.xlsx),*.xlsx")
End If
If xFileName = False Then
Else
  xWb.SaveAs (xFileName)
End If
Application.DisplayAlerts = True
End Sub

3. Press the F5 key to run the code. Then a Save As dialog box pops up, you can see the timestamp displaying in the File name box. Please specify a location to save this file, and then click the Save button. See screenshot:

Notes:

1. The new created Excel file will be saved with the name of current timestamp directly.

2. For those already existing Excel file, the original file name will be replaced with the timestamp.

If you only want to add timestamp within the original file name instead of replacing it, please apply the below VBA code.

VBA code: Save an Excel filename with timestamp (Insert timestamp within original file name)

Sub AddTimestampToFileName()
'Updated by Extendoffice 20191223
Dim xWb As Workbook
Dim xStr As String
Dim xStrOldName As String
Dim xStrDate As String
Dim xFileName As Variant
Dim xFileDlg As FileDialog
Dim i As Variant
Application.DisplayAlerts = False
Set xWb = ActiveWorkbook
xStrOldName = xWb.Name
xStr = Left(xStrOldName, Len(xStrOldName) - 5)
xStrDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
If Right(xStrOldName, 4) = "xlsm" Then
  xFileName = Application.GetSaveAsFilename(xStr & " " & xStrDate, "Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
Else
  xFileName = Application.GetSaveAsFilename(xStr & " " & xStrDate, "Excel Workbook (*.xlsx),*.xlsx")
End If
If xFileName = False Then
Else
  xWb.SaveAs (xFileName)
End If
Application.DisplayAlerts = True
End Sub

arrow blue right bubbleRelated articles:


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Ian · 5 months ago
    Is it possible to embed the code and tell it where to save the file rather than individually select the save path each time?
  • To post as a guest, your comment is unpublished.
    Jason · 10 months ago
    This works except when the user presses the cancel button, it still creates a file called "false".
    • To post as a guest, your comment is unpublished.
      crystal · 7 months ago
      Hi Jason,
      Thank you for reminding me. The codes are updated.
  • To post as a guest, your comment is unpublished.
    hunghk90119@gmail.com · 11 months ago
    Would it be possible to modify it to remove the previous timestamp and save it as in the same directory?
    • To post as a guest, your comment is unpublished.
      crystal · 11 months ago
      Good day,
      Sorry can't help you with that.
  • To post as a guest, your comment is unpublished.
    Nathan · 2 years ago
    You can't save files with : within the filename, if you change this to nbre = Format(Now, "yyyy-mm-dd hh-mm")
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Dear Nathan,
      The below VBA code can help to add the timestamp within the filename.

      Sub test()
      Dim xWb As Workbook
      Dim xStr As String
      Dim xStrOldName As String
      Dim xStrDate As String
      Dim xFileName As String
      Dim xFileDlg As FileDialog
      Dim i As Variant
      Application.DisplayAlerts = False
      Set xWb = ActiveWorkbook
      xStrOldName = xWb.Name
      xStr = Left(xStrOldName, Len(xStrOldName) - 5)
      xStrDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
      If Right(xStrOldName, 4) = "xlsm" Then
      xFileName = Application.GetSaveAsFilename(xStr & " " & xStrDate, "Excel Macro-Enabled Workbook (*.xlsm),*.xlsm")
      Else
      xFileName = Application.GetSaveAsFilename(xStr & " " & xStrDate, "Excel Workbook (*.xlsx),*.xlsx")
      End If
      xWb.SaveAs (xFileName)
      Application.DisplayAlerts = True
      End Sub
  • To post as a guest, your comment is unpublished.
    ELIAS ARROYO · 2 years ago
    I have this below, which works perfect to save file with date stamp, BUT would not work if change to this to add time-> nbre = Format(Now, "yyyy-mm-dd hh:mm")


    nbre = Format(Now, "yyyy-mm-dd")


    Application.DisplayAlerts = False


    ChDir "T:\Distribution Center"
    ActiveWorkbook.SaveAs Filename:="T:\Distribution Center\2. BACKLOG\DC BACKLOG " + nbre + ".xlsx", FileFormat _
    :=xlOpenXMLWorkbook, WriteResPassword:="****", CreateBackup:=False



    Can you please help what is missning???....

    • To post as a guest, your comment is unpublished.
      dude · 11 months ago
      You cannot use the character ":" in file names
  • To post as a guest, your comment is unpublished.
    Matt · 3 years ago
    Is there a version of this that works for Word documents?