Calling a sub before thisworkbook.close

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
229
Office Version
  1. 365
Platform
  1. Windows
I have a macro that i foudn through googling that DOES work but I got a complaint from the other users of the sheet that when it runs it closes ALL spreadsheets
My thought was change the way the workbook is being reference for closing.

Before when i used "thisworkbook.close" "updatemasterAACT" sub ran successfully before closing but when i SWITCH to "workbooks("insertnamehere").close now "updatemasterAACT" errors out because the work book is closing before it finishes running (atleast thats my assessment based on where the macro is saying its failing)

below is the code NOTE: "Workbooks("Customer Complaint Tracker").Close Savechanges:=True" was PREVIOUSLY "Thisworkbook.close Savechanges:=True"

the only reason i have this timer is because people would leave the file open to work on other files preventing others from accessing it.

is there a way i can coordinate the running of the macros so that they fall in order and dont try to run over each other?

VBA Code:
'Make Declarations
Option Explicit
Dim killtime As Variant
Dim procedurename As String

Sub Starttimer()
'Run Stoptimer macro
Stoptimer
'Define killtime
killtime = Now + TimeValue("00:15:00")
'Define Procedurename
procedurename = "Closethisworkbook"
'when time is up Run killtime and procedurename
  Application.OnTime killtime, procedurename

End Sub



Sub Stoptimer()
On Error Resume Next
'When timer is stopped it prevents killtime and procedurename from running
Application.OnTime killtime, procedurename, , False

End Sub

Sub Closethisworkbook()
'Run Stoptimer Macro
Stoptimer
'Closes and saves workbook
UpdatemasterAACT
Workbooks("Customer Complaint Tracker").Close Savechanges:=True

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
If you will never call UpdatemasterAACT at any other time, then move the close statement to that sub? Otherwise do you know for sure which sub is raising the error? If not, use error trapping and create a message box that shows the error number, description and the name of the sub (in Closethisworkbook and in UpdatemasterAACT). You could also try calling a pause sub/function for n seconds between UpdatemasterAACT and Workbooks closing lines. However that is less desirable IMO because 1) you have to guess at for how long to pause; 2) network efficiency can mean that some days the pause isn't long enough.
 
Upvote 0
Quick question. Why 15 minutes? What if the user is still working on the worksheet beyond 15 mins?

Do you think something like this may help?

Code in a Module

VBA Code:
Option Explicit

Public killtime As Variant

Sub Closethisworkbook()
    ThisWorkbook.Close Savechanges:=True
End Sub

Code in ThisWorkbook

VBA Code:
Option Explicit

Private Sub Workbook_Open()
    killtime = Now + TimeValue("00:02:00")

    Application.OnTime killtime, "Closethisworkbook"
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    '~~> User selected a cell. Extend the time
    Application.OnTime EarliestTime:=killtime, Procedure:="Closethisworkbook", Schedule:=False
 
    killtime = Now + TimeValue("00:02:00")
    Application.OnTime killtime, "Closethisworkbook"
End Sub

I set the time as 2 minutes for testing and it works. The logic here is if the user doesn't select any cell in the worksheet for 2 minutes (you can change that) then the user is not working on it.
 
Upvote 0
Micron to answer your question, the sub "updatemasterAACT" is also an assigned macro tied to a button in one of my tabs, the intention why i added it to the close routine was if someone was working on it and had to leave thier desk it would copy the info over to a master spreadsheet that they were working on. it just has to do with how im using particular tabs to call up specific information with out filtering the data in the main sheet. i wanted to keep the master sheet out of hands of the users. i know in the end i can just remove the call for the sub and it will work fine, just trying to make things easy.

I can tell you that it was erroring out on "updatemasterAACT" sub when the timer reached 0. and for some reason it errored out simply trying to unprotect the sheet which is one of the first lines in the sub. so Id have to guess that its erroring because there's no longer a sheet to unlock because the close command did its magic first.

i would like to try the pause line, ive never used that. so i will give it a try and let you all know if that works

Sid;
Ah yes, i have other subs in "thisworkbook" for changes that restart the timer for example when they change tabs, edit a cell, etc its set up that if the workbook sees zero activity for 15 minutes
 
Upvote 0
Sid;
Ah yes, i have other subs in "thisworkbook" for changes that restart the timer for example when they change tabs, edit a cell, etc its set up that if the workbook sees zero activity for 15 minutes

Then it is pretty straight forward. Did you try the approach that I suggested? The above code works without crashing the workbook.
 
Upvote 0
Gave it a shot

however THIS time the updatemaster function worked the way it supposed to but gave me a new error. instead of needed to debug UpdatemasterAACT macro the location switch to the autoclose macro
1659357042554.png


1659356881519.png


at this point if i hit end or stop the macro it just leaves the sheet open
 
Upvote 0
Gave it a shot

however THIS time the updatemaster function worked the way it supposed to but gave me a new error. instead of needed to debug UpdatemasterAACT macro the location switch to the autoclose macro

at this point if i hit end or stop the macro it just leaves the sheet open

If this message is for me then this is not what I suggested...
 
Upvote 0
Sorry Sid that was for Micron, here is code i have in :"THis workbook"

VBA Code:
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

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

and then this is in a seperate module

Code:
'Make Declarations
Option Explicit
Dim killtime As Variant
Dim procedurename As String

Sub Starttimer()
'Run Stoptimer macro
Stoptimer
'Define killtime
killtime = Now + TimeValue("00:01:00")
'Define Procedurename
procedurename = "Closethisworkbook"
'when time is up Run killtime and procedurename
  Application.OnTime killtime, procedurename

End Sub



Sub Stoptimer()
On Error Resume Next
'When timer is stopped it prevents killtime and procedurename from running
Application.OnTime killtime, procedurename, , False

End Sub

Sub Closethisworkbook()
'Run Stoptimer Macro
Stoptimer
'Closes and saves workbook
UpdatemasterAACT

 Application.Wait (Now + TimeValue("00:00:05"))

Workbooks("Customer Complaint Tracker").Close Savechanges:=True

End Sub
 
Upvote 0
Sorry Sid that was for Micron, here is code i have in :"THis workbook"

End Sub[/CODE]

You still have not tried the code that I gave. If I give you a fresh code, are you willing to try it? If not then I will humbly step back.
 
Upvote 0
Sid, if i am following your line of thought

i should change

Code:
'Run Stoptimer macro
Stoptimer
'Define killtime
killtime = Now + TimeValue("00:01:00")
'Define Procedurename
procedurename = "Closethisworkbook"
'when time is up Run killtime and procedurename
  Application.OnTime killtime, procedurename

TO this

VBA Code:
    Application.OnTime EarliestTime:=killtime, Procedure:="Closethisworkbook", Schedule:=False

 

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

    Application.OnTime killtime, "Closethisworkbook

COrrect??
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
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