Skip i'r prif gynnwys

Sut i arbed a chau ffeil Excel yn awtomatig ar ôl amser segur penodol?

Mewn rhai achosion, megis gadael ffeil Excel ar ddisg a rennir, pan fydd defnyddiwr yn dal i agor y ffeil, ni all eraill olygu'r llyfr gwaith. Felly sut i gau'r ffeil ar ôl amser segur penodol er mwyn lleddfu gwaith pobl eraill? Bydd yr erthygl hon yn eich helpu gyda dull VBA.

Auto arbed a chau ffeil Excel ar ôl amser segur penodol gyda chod VBA


Auto arbed a chau ffeil Excel ar ôl amser segur penodol gyda chod VBA

I arbed a chau ffeil Excel yn awtomatig ar ôl amser segur penodol, gwnewch fel a ganlyn.

1. Agorwch y llyfr gwaith sydd ei angen arnoch i'w wneud yn awtomatig wedi'i gadw a'i gau ar ôl amser segur penodol. Yna pwyswch y Alt + F11 allweddi gyda'i gilydd i agor y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

2. Yn y Microsoft Visual Basic ar gyfer Ceisiadau ffenestr, cliciwch ddwywaith ar y Llyfr Gwaith hwn yn y cwarel iawn i agor ffenestr y Cod. Yna copïwch a gludwch islaw cod VBA i mewn i ffenestr y Cod. Gweler y screenshot:

Cod VBA 1: Cadw a chau ffeil Excel yn awtomatig ar ôl amser segur penodol

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. Yna ewch ymlaen i glicio Mewnosod > modiwl, a chopïo a gludo islaw'r cod i mewn i ffenestr y Modiwl. Gweler y screenshot:

Cod VBA 2: Cadw a chau ffeil Excel yn awtomatig ar ôl amser segur penodol

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

4. Gwasgwch y Alt + Q allweddi ar yr un pryd i gau'r Microsoft Visual Basic ar gyfer Ceisiadau ffenestr.

5. Cliciwch Ffeil > Save As > Pori. Gweler y screenshot:

6. Yn y Save As blwch deialog, dewiswch ffolder i achub y ffeil, ei henwi yn ôl yr angen yn y blwch enw Ffeil, dewiswch Llyfr Gwaith Macro-alluogedig Excel oddi wrth y Arbed fel math rhestr ostwng, ac yn olaf cliciwch y Save botwm. Gweler y screenshot:

O hyn ymlaen, bob tro wrth agor y llyfr gwaith hwn, a Kutools ar gyfer Excel bydd blwch deialog yn ymddangos. Rhowch yr amser y byddwch yn ei arbed ac yn cau'r llyfr gwaith yn seiliedig arno, ac yna cliciwch ar y OK botwm.

A bydd y llyfr gwaith yn cael ei gadw a'i gau yn awtomatig ar ôl yr amser segur penodedig hwnnw. Gweler y screenshot:

Offer Cynhyrchiant Swyddfa Gorau

🤖 Kutools AI Aide: Chwyldro dadansoddi data yn seiliedig ar: Cyflawniad Deallus   |  Cynhyrchu Cod  |  Creu Fformiwlâu Personol  |  Dadansoddi Data a Chynhyrchu Siartiau  |  Invoke Swyddogaethau Kutools...
Nodweddion Poblogaidd: Darganfod, Amlygu neu Adnabod Dyblygiadau   |  Dileu Rhesi Gwag   |  Cyfuno Colofnau neu Gelloedd heb Colli Data   |   Rownd heb Fformiwla ...
Super-edrych: VLookup Meini Prawf Lluosog    VLookup Gwerth Lluosog  |   VLookup Ar Draws Taflenni Lluosog   |   Edrych Niwlog ....
Rhestr gwympo Uwch: Creu Rhestr Gollwng yn Gyflym   |  Rhestr Gollwng Dibynnol   |  Rhestr Gollwng Aml-ddewis ....
Rheolwr Colofn: Ychwanegu Nifer Penodol o Golofnau  |  Symud Colofnau  |  Toglo Statws Gwelededd Colofnau Cudd  |  Cymharwch Ystodau a Cholofnau ...
Nodweddion dan Sylw: Ffocws ar y Grid   |  Golwg Dylunio   |   Bar Fformiwla Mawr    Rheolwr Llyfr Gwaith a Thaflen   |  Llyfrgell Adnoddau (Testun Auto)   |  Dewiswr Dyddiad   |  Cyfuno Taflenni Gwaith   |  Amgryptio/Dadgryptio Celloedd    Anfon E-byst trwy Restr   |  Hidlo Super   |   Hidlo Arbennig (hidlo mewn print trwm/italig/strikethrough...) ...
15 Set Offer Gorau12 Testun offer (Ychwanegu Testun, Dileu Cymeriadau,...)   |   50 + Siart Mathau (Siart Gantt,...)   |   40+ Ymarferol Fformiwlâu (Cyfrifwch oedran yn seiliedig ar ben-blwydd,...)   |   19 mewnosod offer (Mewnosod Cod QR, Mewnosod Llun o'r Llwybr,...)   |   12 Trosi offer (Rhifau i Eiriau, Trosi arian cyfred,...)   |   7 Uno a Hollti offer (Rhesi Cyfuno Uwch, Celloedd Hollt,...)   |   ... a mwy

Supercharge Eich Sgiliau Excel gyda Kutools ar gyfer Excel, a Phrofiad Effeithlonrwydd Fel Erioed Erioed. Kutools ar gyfer Excel Yn Cynnig Dros 300 o Nodweddion Uwch i Hybu Cynhyrchiant ac Arbed Amser.  Cliciwch Yma i Gael Y Nodwedd Sydd Ei Angen Y Mwyaf...

Disgrifiad


Mae Office Tab yn dod â rhyngwyneb Tabbed i Office, ac yn Gwneud Eich Gwaith yn Haws o lawer

  • Galluogi golygu a darllen tabbed yn Word, Excel, PowerPoint, Cyhoeddwr, Mynediad, Visio a Phrosiect.
  • Agor a chreu dogfennau lluosog mewn tabiau newydd o'r un ffenestr, yn hytrach nag mewn ffenestri newydd.
  • Yn cynyddu eich cynhyrchiant 50%, ac yn lleihau cannoedd o gliciau llygoden i chi bob dydd!
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