Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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 test()
Dim xWb As Workbook
Dim xStrDate As String
Dim xFileName As String
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
  xWb.SaveAs (xFileName)
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 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

arrow blue right bubbleRelated articles:


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
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.
    Nathan · 1 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 · 1 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 · 1 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.
    Matt · 2 years ago
    Is there a version of this that works for Word documents?