ไปยังเนื้อหาหลัก

วิธีบันทึกชื่อไฟล์ Excel ด้วยการประทับเวลา?

คุณเคยพยายามบันทึกไฟล์ Excel ด้วยการประทับเวลาปัจจุบันหรือไม่? บทความนี้จะแสดงวิธีการเพื่อให้บรรลุ

บันทึกชื่อไฟล์ Excel พร้อมประทับเวลาด้วยรหัส VBA


ลูกศรสีฟ้าฟองขวา บันทึกชื่อไฟล์ Excel พร้อมประทับเวลาด้วยรหัส VBA

คุณสามารถเรียกใช้โค้ด 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 AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 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