Issue with Macro called macro to close WB.

selkov

Well-known Member
Joined
Jan 26, 2004
Messages
787
I have a WB that opens and if left open for 2 minutes runs the macro below to close it. But if I close the workbook before two minutes it still runs the macro and for some reason reopens it and then closes it.

So I tried to stop the second macro from running [WbCls] by checking to be sure the first is open first, but that does not work either.


Can any one help?
Thanks


Sub TMR()
Sheets("EXCWOS").Select
Application.OnTime Now + TimeValue("00:1:30"), "SaveWb"
End Sub

Sub WbCls()
For Each ws In Sheets
If ws.Name = "EXCWOS" Then
Application.DisplayAlerts = False
Workbooks("ExcludedWo").Close (True)
Application.DisplayAlerts = True
Application.Quit
End If
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.
Store the close time in a variable with module scope and cancel the OnTime before closing; something like this:
Code:
Private CloseTime As Date

Sub TMR()
Sheets("EXCWOS").Select
CloseTime = Now + TimeValue("00:01:30")
Application.OnTime CloseTime, "SaveWb"
End Sub

Sub WbCls()
For Each ws In Sheets
If ws.Name = "EXCWOS" Then
Application.DisplayAlerts = False
    On Error Resume Next
    Application.OnTime CloseTime, "SaveWb", , False
    On Error Goto 0
Workbooks("ExcludedWo").Close (True)
Application.DisplayAlerts = True
Application.Quit
End If
End Sub
 
Upvote 0
In the events of thisworkbook:

Code:
Private Sub Workbook_open()
    Call TMR
End Sub
'
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime ahora + TimeValue("00:01:30"), "SaveWb", , Schedule:=False
End Sub
in a module:

Code:
Public ahora
'
Sub TMR()
    Sheets("EXCWOS").Select
    ahora = Now
    Application.OnTime ahora + TimeValue("00:01:30"), "SaveWb"
End Sub
'
Sub SaveWb()
     'here your code
    ThisWorkbook.Save
    ThisWorkbook.Close
End Sub
 
Last edited:
Upvote 0
Found the solution:

Sub SaveWb()
For Each WS In Sheets
If WS.Name = "EXCWOS" Then GoTo prs
GoTo xit
prs:
Application.DisplayAlerts = False
Workbooks("ExcludedWo").Close (True)
Application.DisplayAlerts = True
xit:
Next WS
End Sub
 
Upvote 0
Well now there is another issue. If there is an open excel wb and I open this one and I run the macro it works but leaves an open temporary. The temp file will not close until I close all open excel instances. That is not convenient. Any Idea how to work around that?


Sub SaveWb()
For Each WS In Sheets
If WS.Name = "EXCWOS" Then GoTo prs
GoTo xit
prs:
Application.DisplayAlerts = False
Workbooks("ExcludedWo").Close (True)
Application.DisplayAlerts = True
xit:
Next WS
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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