How to mitigate Macro's latching on to other open workbooks

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
226
Office Version
  1. 365
Platform
  1. Windows
In "ThisWorkbook"
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 Call StopTimer
End Sub
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

Call StopTimer
Call SetTimer
End Sub

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

End Sub

In a seperate module
Code:
Dim DownTime As Date

Sub SetTimer()
    DownTime = Now + TimeValue("00:15:00")
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=True
End Sub

Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=False
 End Sub


Sub ShutDown()
    Application.DisplayAlerts = False
    With Workbooks("Customer Complaint Tracker.xlsm")
        .Saved = True
        .Close
    End With
End Sub

I have this autoclose macro set up in a work book, and it tends to and i dont think this is the proper term but Latched open to where it will re-open the file after it closes it when user has another unrelated excel work book open

is there anything that can be done or an option i can tell them to flag to prevent this?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Thinking about this more and reading some google articles. i am wondering since i am using Sheet Selection event in This workbook could that be some out spilling to other workbooks?
could the act of closing the workbook i want to close then excel bringing another open workbook as active be resetting the timer?
ive noticed sometimes if i access VBA control panel AFTER i close the workbook i can still see that work books macros. is ther a line of code that can purge that until the file is opened again?
 
Upvote 0
A reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
A solution was found in the Cross Post

I am posting a link back to the Cross post after the Solution was determined and tested.

Essentially There were 2 key subs, Starting a Timer and Stopping a timer. each had to have call to each other's subs at the beginning of their code. additionally added a Stop timer to the END of the closing macro. this stopped a chain of Ontime events continuing in the excel application.

Ill let the post explain the thought logic, here is the changes to the code in the seperate module.

VBA Code:
Dim DownTime As Date

Sub SetTimer()
    Call StopTimer
    DownTime = Now + TimeValue("00:15:00")
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=True
End Sub

Sub StopTimer()
    Call SetTimer
    On Error Resume Next
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=False
 End Sub


Sub ShutDown()
    Application.DisplayAlerts = False
    With Workbooks("Customer Complaint Tracker.xlsm")
        .Saved = True
        .Close
    End With
    Call StopTimer
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,543
Messages
6,120,123
Members
448,947
Latest member
test111

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