Gå til hovedindhold

Hvordan gemmes og lukkes projektmappe efter inaktivitet i en vis periode?

I nogle gange kan du ved et uheld lukke en projektmappe, når du har travlt med andre anliggender i lang tid, hvilket kan miste nogle vigtige data i projektmappen. Er der nogle tricks til automatisk at gemme og lukke projektmappen, hvis du har inaktiveret den i en vis periode?

Gem automatisk og luk projektmappe efter inaktivitet i en vis tid med VBA

pil blå højre boble Gem automatisk og luk projektmappe efter inaktivitet i en vis tid med VBA

Der er ingen indbygget funktion i Excel til at løse dette problem, men jeg kan introducere en makrokode, som kan hjælpe dig med at gemme og lukke projektmappe efter inaktivitet på et bestemt tidspunkt.

1. Aktivér den projektmappe, du automatisk vil gemme, og luk efter inaktivitet i nogle sekunder, og tryk på Alt + F11 taster for at åbne Microsoft Visual Basic til applikationer vindue.

2. klik indsatte > Moduler at oprette en Moduler script, og indsæt nedenstående kode til det. Se skærmbillede:

Dim CloseTime As Date
Sub TimeSetting()
    CloseTime = Now + TimeValue("00:00:15")
    On Error Resume Next
    Application.OnTime EarliestTime:=CloseTime, _
      Procedure:="SavedAndClose", Schedule:=True
End Sub
Sub TimeStop()
    On Error Resume Next
    Application.OnTime EarliestTime:=CloseTime, _
      Procedure:="SavedAndClose", Schedule:=False
 End Sub
Sub SavedAndClose()
    ActiveWorkbook.Close Savechanges:=True
End Sub

 

doc gem luk projektmappe efter inaktivitet 1

3. Derefter i Projekt Explorer dobbeltklik på ruden Denne arbejdsbog, og indsæt nedenstående kode i det næste script. Se skærmbillede:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call TimeStop
End Sub

Private Sub Workbook_Open()
    Call TimeSetting
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
   Call TimeStop
   Call TimeSetting
End Sub

 

doc gem luk projektmappe efter inaktivitet 2

4. Gå til dobbeltklik på det modul, du indsatte i trin 2, og tryk på F5 nøgle for at køre koden. Se skærmbillede:
doc gem luk projektmappe efter inaktivitet 3

5. Derefter, efter 15 sekunder, vises en dialogboks, der minder dig om at gemme projektmappen, og klik på Ja for at gemme og lukke projektmappen.
doc gem luk projektmappe efter inaktivitet 4

tips:

(1) I den første kode kan du ændre inaktivitetstiden til anden i denne streng: Nu + tidsværdi ("00:00:15")

(2) Hvis du aldrig har gemt projektmappen før, Gem som dialogboksen kommer først ud og beder dig om at gemme den.
doc gem luk projektmappe efter inaktivitet 5


godt Beskyt regneark

Kutools til Excel Beskyt regneark funktionen kan hurtigt beskytte flere ark eller hele projektmappen på én gang.
doc beskytter flere regneark

Bedste kontorproduktivitetsværktøjer

🤖 Kutools AI Aide: Revolutionér dataanalyse baseret på: Intelligent udførelse   |  Generer kode  |  Opret brugerdefinerede formler  |  Analyser data og generer diagrammer  |  Aktiver Kutools funktioner...
Populære funktioner: Find, fremhæv eller identificer dubletter   |  Slet tomme rækker   |  Kombiner kolonner eller celler uden at miste data   |   Runde uden formel ...
Super opslag: VLookup med flere kriterier    Multiple Value VLookup  |   VOpslag på tværs af flere ark   |   Fuzzy Lookup ....
Avanceret rulleliste: Opret hurtigt rulleliste   |  Afhængig rulleliste   |  Multivælg rulleliste ....
Column Manager: Tilføj et bestemt antal kolonner  |  Flyt kolonner  |  Skift synlighedsstatus for skjulte kolonner  |  Sammenlign områder og kolonner ...
Fremhævede funktioner: Grid fokus   |  Designvisning   |   Stor Formel Bar    Arbejdsbog & Ark Manager   |  Ressourcebibliotek (Autotekst)   |  Datovælger   |  Kombiner regneark   |  Krypter/Dekrypter celler    Send e-mails efter liste   |  Superfilter   |   Specielt filter (filter fed/kursiv/gennemstreget...) ...
Top 15 værktøjssæt12 tekst Værktøjer (tilføje tekst, Fjern tegn, ...)   |   50 + Chart Typer (Gantt kort, ...)   |   40+ Praktisk formler (Beregn alder baseret på fødselsdag, ...)   |   19 Indsættelse Værktøjer (Indsæt QR-kode, Indsæt billede fra sti, ...)   |   12 Konvertering Værktøjer (Tal til ord, Valutaomregning, ...)   |   7 Flet og del Værktøjer (Avancerede kombinere rækker, Opdel celler, ...)   |   ... og mere

Overlad dine Excel-færdigheder med Kutools til Excel, og oplev effektivitet som aldrig før. Kutools til Excel tilbyder over 300 avancerede funktioner for at øge produktiviteten og spare tid.  Klik her for at få den funktion, du har mest brug for...

Beskrivelse


Fanen Office bringer en grænseflade til et kontor med Office, og gør dit arbejde meget lettere

  • Aktiver redigering og læsning af faner i Word, Excel, PowerPoint, Publisher, Access, Visio og Project.
  • Åbn og opret flere dokumenter i nye faner i det samme vindue snarere end i nye vinduer.
  • Øger din produktivitet med 50 % og reducerer hundredvis af museklik for dig hver dag!
Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
When I don't want to edit and I just want to consult, the file still closes. It shouldn't close. Should restart counting when I select cells. What is the solution?
This comment was minimized by the moderator on the site
When I don't want to edit and I just want to consult, the file still closes. It shouldn't close. Should restart counting when I select cells. What is the solution?
This comment was minimized by the moderator on the site
This is great. Any tips on adding a popup message box that will warn the user the sheet is about to close and give them the option to reset the timer?
This comment was minimized by the moderator on the site
I'm not sure what happened but this solution no longer works. Here is the fix to this solution that worked for me:

````
Dim resetCount As Long

Public Sub Workbook_Open()
On Error Resume Next
Set xWB = ThisWorkbook
resetCount = 0
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)On Error Resume Next
Reset
End Sub

Sub Reset()On Error Resume Next
Static xCloseTime
If resetCount <> 0 Then
ThisWorkbook.Application.OnTime xCloseTime, "SaveWork1", Schedule:=False
resetCount = resetCount + 1
xCloseTime = DateAdd("n", 15, Now)
ThisWorkbook.Application.OnTime xCloseTime, "SaveWork1", Schedule:=True

Else
resetCount = resetCount + 1
xCloseTime = DateAdd("n", 15, Now)
ThisWorkbook.Application.OnTime xCloseTime, "SaveWork1", Schedule:=True
End If
End Sub
````
This is using the same SaveWork1 As:
````Sub SaveWork1()
Application.DisplayAlerts = False
ThisWorkbook.Save
ThisWorkbook.Close

Application.DisplayAlerts = True
End Sub

````
This comment was minimized by the moderator on the site
If you are working in a separate workbook at the point where close time is reached then it will close that workbook and not the inactive one. This can be solved by adjusting the code to: - corrected and tested from the below comment - use this code:

Enter into "This Workbook"

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call TimeStop
End Sub
Private Sub Workbook_Open()
Call TimeSetting
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call TimeStop
Call TimeSetting
End Sub


Enter into "module":

Dim CloseTime As Date
Sub TimeSetting()
CloseTime = Now + TimeValue("00:10:00")
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=True
End Sub
Sub TimeStop()
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=False
End Sub
Sub SavedAndClose()
ThisWorkbook.Close Savechanges:=True
End Sub


you can change the time setting by changing CloseTime = Now + TimeValue("00:10:00") - this is set to 10 minutes, change the("00:10:00") to whatever time you want and it works.
This comment was minimized by the moderator on the site
hi i want insert this code to an other code like expiration code with this code how i can do....?
code is...following
Private Sub Workbook_Open()

Dim exdate As Date
Dim i As Integer

'modify values for expiration date here !!!
anul = 2019 'year
luna = 5 'month
ziua = 16 'day

exdate = DateSerial(anul, luna, ziua)

If Date > exdate Then
MsgBox ("The application " & ThisWorkbook.Name & " has expired !" & vbNewLine & vbNewLine _
& "Expiration set up date is: " & exdate & " :)" & vbNewLine & vbNewLine _
& "Contact Administrator to renew the version !"), vbCritical, ThisWorkbook.Name

expired_file = ThisWorkbook.Path & "\" & ThisWorkbook.Name

On Error GoTo ErrorHandler
With Workbooks(ThisWorkbook.Name)
If .Path <> "" Then

.Saved = True
.ChangeFileAccess xlReadOnly

Kill expired_file

'get the name of the addin if it is addin and unistall addin
If Application.Version >= 12 Then
i = 5
Else: i = 4
End If

If Right(ThisWorkbook.Name, i) = ".xlam" Or Right(ThisWorkbook.Name, i) = ".xla" Then
wbName = Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - i)
'uninstall addin if it is installed
If AddIns(wbName).Installed Then
AddIns(wbName).Installed = False
End If
End If

.Close

End If
End With

Exit Sub

End If

'MsgBox ("You have " & exdate - Date & "Days left")
Exit Sub

ErrorHandler:
MsgBox "Fail to delete file.. "
Exit Sub

End Sub
This comment was minimized by the moderator on the site
brilliant thanks
This comment was minimized by the moderator on the site
If you are working in a separate workbook at the point where close time is reached then it will close that workbook and not the inactive one. This can be solved by adjusting the code to:

Dim CloseTime As Date
Dim WKB As String
Sub TimeSetting()
WKB = ActiveWorkbook.Name
CloseTime = Now + TimeValue("00:00:15")
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=True
End Sub
Sub TimeStop()
On Error Resume Next
Application.OnTime EarliestTime:=CloseTime, _
Procedure:="SavedAndClose", Schedule:=False
End Sub
Sub SavedAndClose()
Workbooks(WKB).Close Savechanges:=True
End Sub
This comment was minimized by the moderator on the site
I sometimes run into a "Running time Error" when open the workbook that has this code built into it. Anyway to write this code better for it to be more stable?
This comment was minimized by the moderator on the site
I noticed the same thing. And found the same solution :-)
This comment was minimized by the moderator on the site
The above code is not working when a cell is active. That is

1. enter a value in the cell (don't press Enter or tab)

2. minimize the excel.

In this case the code is not working.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations