如何使用時間戳保存Excel文件名?
您是否曾經嘗試使用當前時間戳保存Excel文件? 本文將向您展示實現它的方法。
使用帶有VBA代碼的時間戳保存Excel文件名
您可以運行下面的VBA代碼以使用時間戳保存Excel文件名。 請執行以下操作。
1.在工作簿中,您需要按當前時間戳進行命名,請按 其他 + F11 同時打開 Microsoft Visual Basic for Applications窗口。
2。 在裡面 Microsoft Visual Basic for Applications 窗口,請點擊 插入 > 模塊。 然後將以下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
相關文章:
- 如何將工作表另存為PDF文件並將其作為附件通過Outlook通過電子郵件發送?
- 如何使用“另存為”功能自動覆蓋Excel中的現有文件?
- 如何在Excel中保存,導出多個/所有工作表到單獨的csv或文本文件?
- 如何在Excel中禁用或不允許“另存為”選項?
- 如何禁用工作簿保存,但只允許在Excel中另存為?
最佳辦公生產力工具
🤖 | Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行 | 生成代碼 | 建立自訂公式 | 分析數據並產生圖表 | 呼叫 Kutools 函數... |
熱門特色: 尋找、突出顯示或識別重複項 | 刪除空白行 | 合併列或儲存格而不遺失數據 | 沒有公式的回合 ... | |
超級查詢: 多條件VLookup | 多值VLookup | 跨多個工作表的 VLookup | 模糊查詢 .... | |
高級下拉列表: 快速建立下拉列表 | 依賴下拉列表 | 多選下拉列表 .... | |
欄目經理: 新增特定數量的列 | 移動列 | 切換隱藏列的可見性狀態 | 比較範圍和列 ... | |
特色功能: 網格焦點 | 設計圖 | 大方程式酒吧 | 工作簿和工作表管理器 | 資源庫 (自動文字) | 日期選擇器 | 合併工作表 | 加密/解密單元格 | 按清單發送電子郵件 | 超級濾鏡 | 特殊過濾器 (過濾粗體/斜體/刪除線...)... | |
前 15 個工具集: 12 文本 工具 (添加文本, 刪除字符,...) | 50+ 圖表 類型 (甘特圖,...) | 40+ 實用 公式 (根據生日計算年齡,...) | 19 插入 工具 (插入二維碼, 從路徑插入圖片,...) | 12 轉化 工具 (數字到單詞, 貨幣兌換,...) | 7 合併與拆分 工具 (高級合併行, 分裂細胞,...) | ... 和更多 |
使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。 點擊此處獲取您最需要的功能...
Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆
- 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
- 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
- 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Sort comments by
#39668
This comment was minimized by the moderator on the site
0
0
#39733
This comment was minimized by the moderator on the site
Report
0
0
#32131
This comment was minimized by the moderator on the site
0
0
#31021
This comment was minimized by the moderator on the site
0
0
#31022
This comment was minimized by the moderator on the site
Report
0
0
#30572
This comment was minimized by the moderator on the site
Report
0
0
#30573
This comment was minimized by the moderator on the site
Report
0
0
#21651
This comment was minimized by the moderator on the site
0
0
#21652
This comment was minimized by the moderator on the site
Report
0
0
#21227
This comment was minimized by the moderator on the site
0
0
#21228
This comment was minimized by the moderator on the site
0
0
#18689
This comment was minimized by the moderator on the site
0
0
There are no comments posted here yet