跳到主要内容

如何使用时间戳保存Excel文件名?

您是否曾经尝试使用当前时间戳保存Excel文件? 本文将向您展示实现它的方法。

使用带有VBA代码的时间戳保存Excel文件名


箭头蓝色右气泡 使用带有VBA代码的时间戳保存Excel文件名

您可以运行下面的VBA代码以使用时间戳保存Excel文件名。 请执行以下操作。

1.在工作簿中,您需要按当前时间戳进行命名,请按 其他 + F11 同时打开 Microsoft Visual Basic for Applications窗口。

2.在 Microsoft Visual Basic应用程序 窗口,请点击 插页 > 模块。 然后将以下VBA代码复制到 代码 窗口。 看截图:

VBA代码:保存带有时间戳的Excel文件名(用时间戳替换文件名)

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。 按 F5 键来运行代码。 然后一个 另存为 对话框弹出,您可以看到时间戳显示在文件名框中。 请指定保存此文件的位置,然后单击 保存 按钮。 看截图:

:

1.新创建的Excel文件将直接以当前时间戳的名称保存。

2.对于那些已经存在的Excel文件,原始文件名将被替换为时间戳。

如果您只想添加时间戳后跟原始文件名而不是替换它,请应用以下 VBA 代码。

VBA 代码:保存带有时间戳的 Excel 文件名(插入时间戳,后跟原始文件名)

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

箭头蓝色右气泡相关文章:

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (12)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
To whom it may concern,
I try to implied your code "VBA code: Save an Excel filename with timestamp (Insert timestamp within original file name)" at https://www.extendoffice.com/documents/excel/4413-excel-timestamp-filename.html. BUT it stop at new file name with timestamp Save As dialogue box. If I want to auto save continuous after Save As shown dialogue box, Close all workbook and close application. What VBA code you recommend to end this job? Please help.

Please answer me at misterxyz123@gmail
This comment was minimized by the moderator on the site
Hi Mr.X,
After running the code, all opened workbooks (except for the currently using workbook) are saved as new files with timestamp followed by the original file name then closed automatically. Please give it a try. Thank you.
Please specify a folder to save the files in this line: xPath = "C:\Users\Win10x64Test\Desktop\file with timestamp\"
Sub AddTimestampToFileName()
    'Updated by Extendoffice 20220826
    Dim xWb As Workbook
    Dim xStr As String
    Dim xPath As String
    Dim xStrDate As String
    Dim xFileName As String
    Application.DisplayAlerts = False
    
    
    xPath = "C:\Users\Win10x64Test\Desktop\file with timestamp\"
    xFileName = ActiveWorkbook.Name
    xStrDate = " " & Format(Now, "yyyy-mm-dd hh-mm-ss")
    For Each xWb In Workbooks
        If xWb.Name <> xFileName Then
            If Dir(xWb.FullName) <> "" Then
                xStr = Left(xWb.Name, InStrRev(xWb.Name, ".") - 1)
                xWb.SaveAs Filename:=xPath & Replace(xWb.Name, xStr, xStr & xStrDate)
            Else
                xWb.SaveAs Filename:=xPath & xWb.Name & xStrDate & ".xlsx"
            End If
        xWb.Close
        End If
    Next
    
    xStr = Left(xFileName, InStrRev(xFileName, ".") - 1)
    ActiveWorkbook.SaveAs Filename:=xPath & Replace(xFileName, xStr, xStr & xStrDate)
    Application.DisplayAlerts = True
    
End Sub
This comment was minimized by the moderator on the site
Is it possible to embed the code and tell it where to save the file rather than individually select the save path each time?
This comment was minimized by the moderator on the site
This works except when the user presses the cancel button, it still creates a file called "false".
This comment was minimized by the moderator on the site
Hi Jason,
Thank you for reminding me. The codes are updated.
This comment was minimized by the moderator on the site
Would it be possible to modify it to remove the previous timestamp and save it as in the same directory?
This comment was minimized by the moderator on the site
Good day,
Sorry can't help you with that.
This comment was minimized by the moderator on the site
You can't save files with : within the filename, if you change this to nbre = Format(Now, "yyyy-mm-dd hh-mm")
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
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???....
This comment was minimized by the moderator on the site
You cannot use the character ":" in file names
This comment was minimized by the moderator on the site
Is there a version of this that works for Word documents?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations