Help with Autoclosing Excel after inactivity

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
229
Office Version
  1. 365
Platform
  1. Windows
I am having issues running some new coding that I found from googling.

All in all the code DOES work fine but its when I ADDED in "Workbook_beforeclose"
UpdatemasterAACT
ThisWorkbook.Save

Where updatemasterAACT is another Macro i have that is tied to a button. When you run it on its own perfectly fine, never faults
When i CLICK but X button to close excel, Runs it just fine never faults

but when the TIMER runs out to close excel due to inactivity THEN i start getting run time errors when UpdatemasterrAACT macro runs.

I am not sure why trying to close Excel on a timer vs closing excel intentionally could cause code to error. I see zero reason to alter the code in UpdatemasterAACT if it can run fine with out errors normally So i BELIEVE my solution is changing something in the code I posted Below

Following code is in a standard module


VBA Code:
Option Explicit
Dim killtime As Variant
Dim procedurename As String

Sub Starttimer()

Stoptimer

killtime = Now + TimeValue("00:10:00")

procedurename = "Closethisworkbook"

  Application.OnTime killtime, procedurename

End Sub

Sub Startconfirm()

Stoptimer

killtime = Now + TimeValue("00:00:10")

procedurename = "CloseWorkbook"

  Application.OnTime killtime, procedurename


End Sub

Sub Stoptimer()
On Error Resume Next

Application.OnTime killtime, procedurename, , False

End Sub

Sub Closethisworkbook()

Stoptimer

ThisWorkbook.Close Savechanges:=True

End Sub

The next set of Code is in "Thisworkbook"

Code:
Private Sub Workbook_Activate()
 
 Starttimer
 
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Stoptimer
UpdatemasterAACT
ThisWorkbook.Save
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

 Starttimer

End Sub

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

 Starttimer

End Sub

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

 Starttimer

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I'm guessing this line isn't right....
Code:
procedurename = "CloseWorkbook"
Should be "Closethisworkbook". HTH. Dave
 
Upvote 0
OMG it took me days to realize this....the macro to close it happening right before "before close" call to the update macro so the sheet is closing when the macro is asking to reference it.

That Problem is solved i added the macro i wanted to run before "ThisworkbookClose" command in "Closethisworkbook" Sub and now it runs with out error HOWEVER

I wonder if theres a way i can still have this macro run on both Closethisworkbook Sub AND clicking the X in upper right with out the 2 fighting each other?
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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