VBA to automatically save and close an Excel file after a period of inactivity in the file

VANCOUVER_RON

New Member
Joined
Apr 23, 2019
Messages
3
I want to automatically close an Excel file after a period of inactivity. This was helpful as a start, but...


Not precisely what I'm looking for. It appears the ''timer" starts running when you open the Excel file and doesn't stop. Then the "ballistic" code saves and closes the file
automatically after the timer runs out, even if you're actively using the Excel file.

I need to "reset" the timer every time the user is actively using the Excel file--inserting data, updating a form--doing anything in the file resets the timer. It's only after a
period of time of inactivity (e.g., one hour of inactivity) that I want the "shut down" timer to begin. For example, if the file is inactive for an hour, then the timer
(e.g., 30 seconds) begins running and the file automatically closes and saves after the 30 second timer runs out.

In other words, activity in the file continually resets the shut-down timer. That's what I'm looking for.
 
Well, that would work, except the computer which shows the spreadsheet on the big TV screen will have to remain Shared. Otherwise, if that PC was moved to one of the worker's desks, then constantly, other workers would request to Share the file so then other workers can update, save then close.

I have another idea,

If the timer does not work in shared workbooks, then can the file save after every edit of the spreadsheet? The updates are usually updates to one to two cells so it will not be many. Can this be a MACRO in "ThisWorkbook" that can accomplish this?
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Actually it's shared because a PC is used to show the spreadsheet on the big screen.

I have an idea, can a MACRO be used while shared to save every time any cell gets edited?
 
Upvote 0
I found the solution to incorporate sorting and saving (which both had the same issue when using timers in shared workbook). This will not only save on each edit, it will also resort then save again to auto-sort in shared file (it works):

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

ThisWorkbook.Save

ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add2 Key:= _
Range("O4:O1000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

ThisWorkbook.Save

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top