VBA to autosave and close after a set amount of time

lojanica

New Member
Joined
Feb 22, 2024
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have found a post from back in 2020 which shows some examples of how to do this and the new Excel document works perfectly

Here is the code for the Module1.
VBA Code:
Dim DownTime As Date

Sub SetTimer()
    DownTime = Now + TimeValue("00:01: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
    ThisWorkbook.Close SaveChanges:=True
End Sub

Here is the code under ThisWorkbook:

VBA Code:
Private Sub Workbook_Open()
    Call SetTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call StopTimer
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Call StopTimer
    Call SetTimer
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
  ByVal Target As Excel.Range)
    Call StopTimer
    Call SetTimer
End Sub

As mentioned, codes work perfectly on new documents.
However, I would like to implement it in the document, which already has code under ThisWorkbook.
Please see the below code under ThisWorkbook in my document.
VBA Code:
Private Sub Workbook_Open()

ThisWorkbook.Sheets("AMSI-R-102 Job Request Register").Range("B6").Value = Environ("USERNAME")

 Call SetTimer

    Dim mainwb As Workbook
    Dim usernameSheetName As String
    Dim targetSheet As Worksheet
    
    Set mainwb = ActiveWorkbook
    usernameSheetName = mainwb.Sheets("AMSI-R-102 Job Request Register").Range("B6").Value
        
    On Error Resume Next
    Set targetSheet = mainwb.Sheets(usernameSheetName)
    On Error GoTo 0
    
    If Not targetSheet Is Nothing Then
        targetSheet.Activate
    Else
        mainwb.Sheets("AMSI-R-102 Job Request Register").Activate
        Exit Sub
    End If

    If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
        ActiveSheet.ShowAllData
    End If

    Range("A6:I6").AutoFilter Field:=8, Criteria1:=Range("B6")
    Range("A6:I6").AutoFilter Field:=7, Criteria1:="In progress"
    
    lastRow = Cells(Rows.Count, 2).End(xlUp).Row
    Range("A8:I" & lastRow).Sort Key1:=Range("D8:D" & lastRow), Order1:=xlAscending, Header:=xlNo

End Sub



Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call StopTimer
End Sub
Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Call StopTimer
    Call SetTimer
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
  ByVal Target As Excel.Range)
    Call StopTimer
    Call SetTimer
End Sub

As you can see, I have moved "Call SetTimer" to the existing Private Sub Workbook_Open().
The problem is that when the timer is up, the document closes and opens again with a message from the attached photo.
If I press Enable Macro, it goes in a loop, opening and closing the document.
If I press disable Macro, an error message appears. When I press okay, it pops up again and again.

My existing code is affecting it, but I cannot see what is causing these issues.
Any Suggestions?
 

Attachments

  • Error if Disable Macro press .jpg
    Error if Disable Macro press .jpg
    16.7 KB · Views: 9
  • Security Notice .jpg
    Security Notice .jpg
    45.3 KB · Views: 10

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I assume that your vba project also include Module1 with its listed code and nothing more on it

Let's try adding some debug code in the several subs, as follows:
In ThisWorkbook module:
VBA Code:
Private Sub Workbook_Open()
ThisWorkbook.Sheets("AMSI-R-102 Job Request Register").Range("B6").Value = Environ("USERNAME")
'
Stop                                           '<<< See text
 Debug.Print ">>>>"
 Debug.Print "Opening", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
 Call SetTimer
 '....
 'etc
 
 
 
 
 Private Sub Workbook_BeforeClose(Cancel As Boolean)
'
Debug.Print "BeforeClose", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
    Call StopTimer
End Sub

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
'
  Debug.Print "Calculating", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
    Call StopTimer
    Call SetTimer
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
  ByVal Target As Excel.Range)
'
  Debug.Print "SelectionChange", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
    Call StopTimer
    Call SetTimer
End Sub

In Module1
VBA Code:
Dim DownTime As Date

Sub SetTimer()
    DownTime = Now + TimeValue("00:01:00")
    Application.OnTime EarliestTime:=DownTime, _
    Procedure:="ShutDown", Schedule:=True
Debug.Print "SetTimer", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
End Sub


Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=False
Debug.Print "STOPTimer", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
End Sub


Sub ShutDown()
Debug.Print "ShutDown", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
    Application.DisplayAlerts = False
    ThisWorkbook.Close SaveChanges:=True
End Sub
Note the various "Debug.Print" that have been added and the Stop, the remaining code is not affected

After the file has been modified and saved, close and reopen Excel, then reopen the file and execute once max twice the process: take note of what happens and at what time. Finally we should go to the "Immediate window" of the vba and examine what has been logged, compare that against the teorethical flow and against the manual log.
Note that I got the impression that in this process you'll enter and endless loop, that's why I inserted also a Stop at the beginning of the Workbook_Open procedure: when the macro stops on the Stop then press F5 to continue the testing or stop running the macro if you are in a loop.
To access the vba Immediate window: press Contr-g (while in the vba window), or use Menu /Display /Immediate window
If you share the content of the Immediate window, along with the description of what happened during the test maybe we can speculate about the possible couses together

The added code is intended only for debug purspose, it shoul be removed from the final code (debug.print at each Calculate would be quite slow)
 
Upvote 0
I assume that your vba project also include Module1 with its listed code and nothing more on it

Let's try adding some debug code in the several subs, as follows:
In ThisWorkbook module:
VBA Code:
Private Sub Workbook_Open()
ThisWorkbook.Sheets("AMSI-R-102 Job Request Register").Range("B6").Value = Environ("USERNAME")
'
Stop                                           '<<< See text
 Debug.Print ">>>>"
 Debug.Print "Opening", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
 Call SetTimer
 '....
 'etc
 
 
 
 
 Private Sub Workbook_BeforeClose(Cancel As Boolean)
'
Debug.Print "BeforeClose", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
    Call StopTimer
End Sub

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
'
  Debug.Print "Calculating", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
    Call StopTimer
    Call SetTimer
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
  ByVal Target As Excel.Range)
'
  Debug.Print "SelectionChange", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
    Call StopTimer
    Call SetTimer
End Sub

In Module1
VBA Code:
Dim DownTime As Date

Sub SetTimer()
    DownTime = Now + TimeValue("00:01:00")
    Application.OnTime EarliestTime:=DownTime, _
    Procedure:="ShutDown", Schedule:=True
Debug.Print "SetTimer", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
End Sub


Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=DownTime, _
      Procedure:="ShutDown", Schedule:=False
Debug.Print "STOPTimer", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
End Sub


Sub ShutDown()
Debug.Print "ShutDown", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime
    Application.DisplayAlerts = False
    ThisWorkbook.Close SaveChanges:=True
End Sub
Note the various "Debug.Print" that have been added and the Stop, the remaining code is not affected

After the file has been modified and saved, close and reopen Excel, then reopen the file and execute once max twice the process: take note of what happens and at what time. Finally we should go to the "Immediate window" of the vba and examine what has been logged, compare that against the teorethical flow and against the manual log.
Note that I got the impression that in this process you'll enter and endless loop, that's why I inserted also a Stop at the beginning of the Workbook_Open procedure: when the macro stops on the Stop then press F5 to continue the testing or stop running the macro if you are in a loop.
To access the vba Immediate window: press Contr-g (while in the vba window), or use Menu /Display /Immediate window
If you share the content of the Immediate window, along with the description of what happened during the test maybe we can speculate about the possible couses together

The added code is intended only for debug purspose, it shoul be removed from the final code (debug.print at each Calculate would be quite slow)

Morning Anthony

Thank you for getting back, I have to admit it took me few goes to do exactly what you were asking, however i think i have done it all.

So lets start from beginning, originally i had some code in Model1 which i moved to Model 2.

In the In ThisWorkbook module I have entered following code:



VBA Code:
Private Sub Workbook_Open()

ThisWorkbook.Sheets("AMSI-R-102 Job Request Register").Range("B6").Value = Environ("USERNAME")

'

Stop '<<< See text

Debug.Print ">>>>"

Debug.Print "Opening", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime

Call SetTimer



Dim mainwb As Workbook

Dim usernameSheetName As String

Dim targetSheet As Worksheet



Set mainwb = ActiveWorkbook

usernameSheetName = mainwb.Sheets("AMSI-R-102 Job Request Register").Range("B6").Value



On Error Resume Next

Set targetSheet = mainwb.Sheets(usernameSheetName)

On Error GoTo 0



If Not targetSheet Is Nothing Then

targetSheet.Activate

Else

mainwb.Sheets("AMSI-R-102 Job Request Register").Activate

Exit Sub

End If



If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then

ActiveSheet.ShowAllData

End If



Range("A8:N8").AutoFilter Field:=13, Criteria1:=Range("B6")

Range("A8:N8").AutoFilter Field:=12, Criteria1:="In progress"



lastRow = Cells(Rows.Count, 2).End(xlUp).Row

Range("A8:N" & lastRow).Sort Key1:=Range("F8:F" & lastRow), Order1:=xlAscending, Header:=xlNo



End Sub





Private Sub Workbook_BeforeClose(Cancel As Boolean)

'

Debug.Print "BeforeClose", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime

Call StopTimer

End Sub



Private Sub Workbook_SheetCalculate(ByVal Sh As Object)

'

Debug.Print "Calculating", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime

Call StopTimer

Call SetTimer

End Sub



Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _

ByVal Target As Excel.Range)

'

Debug.Print "SelectionChange", Format(Now, "hh:mm:ss"), "DownTime is: " & DownTime

Call StopTimer

Call SetTimer

End Sub



Upon opening the document and enabling content VB window open with highlight yellow on “Stop '<<< See text”

After pressing F5 code run without errors

After 1 minute file and Excel closed.

Upon reopening widows then file I had a screen as per screenshot “After first run of code and reopening file” I press enable content and VB window popped up then press F5 again.

After 1 min file closed and widows stayed open as per screen shot “Code run second time and close file with this window” after pressing Enable Macro file reopened then closed and stayed closed with Excel remained open.

Bellow is Immediate
Rich (BB code):
SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:10 DownTime is:

STOPTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:10 DownTime is:

STOPTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:10 DownTime is:

STOPTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:10 DownTime is:

STOPTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:10 DownTime is:

STOPTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:10 DownTime is:

STOPTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:10 DownTime is:

STOPTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:10 DownTime is:

STOPTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:10 DownTime is:

STOPTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:10 DownTime is: 24/04/2024 9:47:10 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:10 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SelectionChange 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:11 DownTime is:

STOPTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:11 DownTime is: 24/04/2024 9:47:11 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:11 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

Calculating 09:46:12 DownTime is:

STOPTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM

SetTimer 09:46:12 DownTime is: 24/04/2024 9:47:12 AM


Taken after first run and reopening the file

-----------------------------------------------------------------------------------------------------------------

Did this 3 times same results.

It is wroth to mention that after pressing F5 I stayed on VB window till it closed, I believe this is important because of what happens below

However I wen ahead and removed “Stop from code and reopen document. Code run without errors closing and saving file. No additional message boxes pop ups

Went back and renter “Stop” to code, same thing happened as first time I tried.

Once again removed Stop from code, once again code runes as designed.
I hope above makes sense :)
 

Attachments

  • Code run second time and close file with this window.jpg
    Code run second time and close file with this window.jpg
    76.8 KB · Views: 1
  • After first run of code and reopening file .jpg
    After first run of code and reopening file .jpg
    142.8 KB · Views: 2
Upvote 0
Please do your best to keep the code compact, without extra lines that make difficult understanding where a macro starts and where is the next one.

Did this 3 times same results.
It is wroth to mention that after pressing F5 I stayed on VB window till it closed, I believe this is important because of what happens below
However I wen ahead and removed “Stop from code and reopen document. Code run without errors closing and saving file. No additional message boxes pop ups
Went back and renter “Stop” to code, same thing happened as first time I tried.
Once again removed Stop from code, once again code runes as designed.
Sorry, do you mean "Everithing works ok" or "It doesn't work"

Unfortunately the Immediate window shows only Calculate related logs that occours in a couple of seconds starting at 9:46:10 (that window only keeps the last 200 lines), so also that log doesn't give me a clear view of the situation.

However one thing is clear: the variable DownTime is not visible from ThisWorkbook
This is because DownTime is declared (on top of Module1) with a Dim statement: this makes the variable visible within the whole Module1 BUT not from other Modules
I had to realize about that when I went for the first suggestion! :(

To make the variable visible also from ThisWorkbook module it has to be declared on top of a standard module using the keyword Public (and not Dim, that implies it is visible only within the module)
Code:
Public Downtime As Date

1) This is the first modification to be done on your code. It will enable the Workbook_BeforeClose macro to descheduled Sub Shutdown

2) The second modification is removing completely the current Workbook_SheetCalculate: that event occours quite often and does not always refers to a user operation. I think the Workbook_SheetSelectionChange should be enough to intercept user activity. Maybe you can add later a 10 seconds notice that the workbook is going to be closed with a "Please don't do that" button to delay the process.
To temporary remove Workbook_SheetCalculate, just rename it Workbook_SheetCalculateZZ and it will no longer start. We'll delete it later, if really useless.

3) The Stop in Sub Workbook_Open was inserted to let you understand if the process had entered in a loop, ie the file reopens by itself after closed; which was your impression?

4) Also now it is evident from one of the images that Excel closes by itself; in other words it crashes.
I guess this is due to some uncontrolled operations during the file closing. The first candidates is Workbook_SheetCalculate, as it starts during file save; having removed the handling of that event it should be no longer a potential problem.
The second candidate is Shutdown itself, therefore let's stop handling the events during execution of its code by adding Application.EnableEvents = True /False as follows
Code:
Sub ShutDown()
Application.EnableEvents = False
    Application.DisplayAlerts = False
    ThisWorkbook.Close SaveChanges:=True
Application.EnableEvents = True
End Sub
This is the third modification

To apply the above changes it is better to disable Excel events so you can work safely; I suggest the following procedure, after Excel (the application) is started:
-go to the vba and open the Immediate window
-type the following command in that window:
Code:
Application.EnableEvents=False
-Now open the file; the WorkbookOpen macro should not start, as well other events should not trigger any of the macro.
-Make the described modifications (if you had removed the Stop at the WorkbookOpen macro, then reinsert it); save and close the file; close the application (Excel), then reopen Excel (this will reenable the "events")

At this point we should be ready for testing again the process. So:
-open the file; take note of hh:mm:ss (from the PC clock, please)
-press F5 to continue after the Stop
-do nothing and wait for 1 minute: the file should close, Excel should remain open
-go to the vba Immediate window and copy its content, from beginning to the end
-wait for a couple of minutes, nothing should happen
-reopen the file, press F5 to overcome the Stop;
-play on the file by selecting here and there or entering some data and keep it "busy" for 3 minutes; the file should stay open
-go to the vba Immediate window and copy again its content, from beginning to the end
-stop playing with the file and check that after 1 minute it closes and Excel stay open
If things went as described above then I would say that "It works" (do you agree?)
If something else occours then take notes of what happens and at what time (hh:mm:ss please, from the Pc clock) and copy the content of the Immediate window at the moment; then put everithing in your next message

Don't remove the Stop in the Sub Workbook_Open until you decide that it works according the expectations, or until we give up
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,612
Members
449,109
Latest member
Sebas8956

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