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

วิธีบันทึกและปิดไฟล์ Excel โดยอัตโนมัติหลังจากช่วงเวลาที่ไม่ได้ใช้งาน

ในบางกรณีเช่นการทิ้งไฟล์ Excel ไว้บนดิสก์ที่แชร์เมื่อไฟล์ยังคงเปิดอยู่โดยผู้ใช้คนอื่นจะไม่สามารถแก้ไขเวิร์กบุ๊กได้ ดังนั้นจะปิดไฟล์โดยอัตโนมัติหลังจากช่วงเวลาที่ไม่ได้ใช้งานเพื่อความสะดวกในการทำงานของผู้อื่นได้อย่างไร? บทความนี้จะช่วยคุณเกี่ยวกับวิธี VBA

บันทึกและปิดไฟล์ Excel โดยอัตโนมัติหลังจากช่วงเวลาที่ไม่ได้ใช้งานโดยใช้รหัส VBA


บันทึกและปิดไฟล์ Excel โดยอัตโนมัติหลังจากช่วงเวลาที่ไม่ได้ใช้งานโดยใช้รหัส VBA

หากต้องการบันทึกและปิดไฟล์ Excel โดยอัตโนมัติหลังจากช่วงเวลาที่ไม่ได้ใช้งานโปรดทำดังนี้

1. เปิดสมุดงานที่คุณต้องการเพื่อให้บันทึกและปิดโดยอัตโนมัติหลังจากช่วงเวลาที่ไม่ได้ใช้งาน จากนั้นกดปุ่ม อื่น ๆ + F11 คีย์ร่วมกันเพื่อเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2 ใน Microsoft Visual Basic สำหรับแอปพลิเคชัน โปรดดับเบิลคลิกที่ไฟล์ สมุดงานนี้ ในบานหน้าต่างด้านขวาเพื่อเปิดหน้าต่างรหัส จากนั้นคัดลอกและวางโค้ด VBA ด้านล่างลงในหน้าต่างรหัส ดูภาพหน้าจอ:

รหัส VBA 1: บันทึกและปิดไฟล์ Excel โดยอัตโนมัติหลังจากช่วงเวลาที่ไม่ได้ใช้งาน

Dim xTime As String
Dim xWB As Workbook

Private Sub Workbook_Open()
'Updated by Extendoffice 2019/1/20
    On Error Resume Next
    xTime = Application.InputBox("Please specify the idle time:", "KuTool For Excel", "00:00:20", , , , , 2)
    Set xWB = ActiveWorkbook
    If xTime = "" Then Exit Sub
    Reset
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    On Error Resume Next
    If xTime = "" Then Exit Sub
    Reset
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    On Error Resume Next
    If xTime = "" Then Exit Sub
    Reset
End Sub

Sub Reset()
    Static xCloseTime
    If xCloseTime <> 0 Then
        ActiveWorkbook.Application.OnTime xCloseTime, "SaveWork1", , False
    End If
    xCloseTime = Now + TimeValue(xTime)
    ActiveWorkbook.Application.OnTime xCloseTime, "SaveWork1", , True
End Sub

3. จากนั้นไปที่คลิก สิ่งที่ใส่เข้าไป > โมดูลแล้วคัดลอกและวางโค้ดด้านล่างลงในหน้าต่างโมดูล ดูภาพหน้าจอ:

รหัส VBA 2: บันทึกและปิดไฟล์ Excel โดยอัตโนมัติหลังจากช่วงเวลาที่ไม่ได้ใช้งาน

Sub SaveWork1()
'Updated by Extendoffice 2019/1/20
    Application.DisplayAlerts = False
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    
    Application.DisplayAlerts = True
End Sub

4 กด อื่น ๆ + Q พร้อมกันเพื่อปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

5 คลิก เนื้อไม่มีมัน > บันทึกเป็น > หมวดหมู่สินค้า. ดูภาพหน้าจอ:

6 ใน บันทึกเป็น โปรดเลือกโฟลเดอร์ที่จะบันทึกไฟล์ตั้งชื่อตามที่คุณต้องการในกล่องชื่อไฟล์เลือก สมุดงานที่เปิดใช้งานแมโคร Excel จาก บันทึกเป็น ชนิด รายการแบบหล่นลงแล้วคลิกไฟล์ ลด ปุ่ม. ดูภาพหน้าจอ:

ตั้งแต่นี้เป็นต้นไปทุกครั้งที่เปิดสมุดงานนี้ก Kutools สำหรับ Excel กล่องโต้ตอบจะปรากฏขึ้น โปรดป้อนเวลาที่คุณจะบันทึกและปิดสมุดงานตามจากนั้นคลิกที่ไฟล์ OK ปุ่ม

และสมุดงานจะถูกบันทึกและปิดโดยอัตโนมัติหลังจากเวลาว่างที่ระบุไว้ ดูภาพหน้าจอ:

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 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 (30)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hello good afternoon! I would like MSBox not to appear when opening the file. I don't want them to change the configured closing time.
This comment was minimized by the moderator on the site
The Problem is it`s a file with links and every time you start it, it will ask you if you would update it and read only mode?
This comment was minimized by the moderator on the site
Hello
Code works but if i close the file before the time runs out, It will open it again and close. how can i stop the code when i close the file manual.
This comment was minimized by the moderator on the site
Hi,

I tested the code and it did not reproduce the problem you mentioned. Can I ask which version of Excel you are using?
This comment was minimized by the moderator on the site
Hi
It is Microsoft Excel 365 MSO (Version 2308) 32 bit.
This comment was minimized by the moderator on the site
This is my Code:

Dim xTime As String
Dim xWB As Workbook

Private Sub Workbook_Open()
On Error Resume Next
xTime = "00:00:35"
Set xWB = Workbooks("Produktionsbericht - Kopie.xlsm")
If xTime = "" Then Exit Sub
Reset
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
On Error Resume Next
If xTime = "" Then Exit Sub
Reset
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
If xTime = "" Then Exit Sub
Reset
End Sub

Sub Reset()
Static xCloseTime
If xCloseTime <> 0 Then
xWB.Application.OnTime xCloseTime, "SaveWork1", , False
End If
xCloseTime = Now + TimeValue(xTime)
xWB.Application.OnTime xCloseTime, "SaveWork1", , True
End Sub
This comment was minimized by the moderator on the site
Hello
All works at my file but i have a new Problem.
If i close the file before the time runs out, it will open it again and close it when the time runs out.
Can i stop the VBA Code when i close the File myself?
This comment was minimized by the moderator on the site
Code is not working if the module is protected, is there any way to get the code run if module is protected.
This comment was minimized by the moderator on the site
Hi Chinna,

If the module is protected, the code is hidden and cannot be run. Sorry for the inconvenience.
This comment was minimized by the moderator on the site
Perdonen, si se pudiera modificar el código o adaptarlo para que reaccione con formularios (UserForm), porque solo reacciona al estar activo en las celdas pero no en los formularios, gracias
This comment was minimized by the moderator on the site
Hi Moises Peraza,
UserForm does not affect the use of the VBA code. Can you describe the problem you encountered more clearly.
This comment was minimized by the moderator on the site
Gracias por responder, el inconveniente que tengo es: le pongo de tiempo de inactividad 2 min. En el momento de trabajar con formularios (ingresar información del cliente), pasado ese tiempo me cierra o me tira el mensaje que se cerrara, espero darme a entender
This comment was minimized by the moderator on the site
Hi mister, how to view the countdown?
This comment was minimized by the moderator on the site
Hi mister, how to view the countdown?
This comment was minimized by the moderator on the site
(Sigh.)
Have a look at the code.
If you put this VBA into Excel001.xlsm, but, when the timeout occurs, you have Excel002.xlsm as the "active" workbook, then it's actually going to close THAT Excel file, rather than the one you've put the VBA into.
This comment was minimized by the moderator on the site
look below, Mark's comment : "I would recommend identifying the workbook with the workbook file name. This would allow the users to have multiple workbooks open and closing out only the specific workbook that needs to have the time lock.

Suggestion: Set xwb = Workbooks("<file name>")
instead of: Set xwb = ActiveWorkbook

Also, replace all references to "ActiveWorkbook" to the Xwb in the module

Great code otherwise."
This comment was minimized by the moderator on the site
Hi. I need to set my shared spreadsheet on the office network to auto save and close when I'm done with it for the day. Only my boss and I are supposed to make and save changes, but several managers view it daily.
I need to be able to have it auto save for the two of us, while not saving for other users. Is this possible? Can I set the macro to work only for us? Or will it effect everyone?
I know how to protect the sheet, but I can't do that, because they want to be able to make some temporary changes from time to time. I just need to be able restrict auto save to specific users.
This comment was minimized by the moderator on the site
Is there a way to change this so it prompts the user to save/close the file, instead of automatically saving and closing the file?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations