VBA Code Forcing File to Reopen when closed

TheRobRush

New Member
Joined
Nov 5, 2018
Messages
39
I have the following code in my modules. It works great, for what it was intended to do. (Keep a running clock for scheduling purposes, break meal penalties etc)

The issue arises if you have two copies of the file open at the same time.

When two files with this code in it ARE open, when you close one it will immediately REOPEN itself.

Any ideas?

Code:
Dim SchedRecalc As Date



Sub Recalc()

With Range("CG1")
.Value = Format(Time, "hh:mm:ss AM/PM")
End With
Call SetTime
End Sub
Sub SetTime()
SchedRecalc = Now + TimeValue("00:00:01")
Application.OnTime SchedRecalc, "Recalc"
End Sub



Sub Disable()
On Error Resume Next
Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Recalc", Schedule:=False
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Only code in workbook close is hiding and unhiding of sheets.


Don't think issue is there. BUT just in case.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

' Hides all worksheets except the control panel before closing
' created by Rob Rush 2018

Application.ScreenUpdating = False
Worksheets("CONTROL PANEL").Visible = True
Worksheets("BOARD GENERATOR").Visible = xlSheetVeryHidden
Worksheets("Ticket Leads").Visible = xlSheetVeryHidden
Worksheets("Admissions Leads").Visible = xlSheetVeryHidden
Worksheets("UBO Leads").Visible = xlSheetVeryHidden
Worksheets("Booth 1").Visible = xlSheetVeryHidden
Worksheets("Booth 2").Visible = xlSheetVeryHidden
Worksheets("Booth 3").Visible = xlSheetVeryHidden
Worksheets("Booth 4").Visible = xlSheetVeryHidden
Worksheets("Will Call").Visible = xlSheetVeryHidden
Worksheets("Admissions Hosts").Visible = xlSheetVeryHidden
Worksheets("UBO Associates").Visible = xlSheetVeryHidden
Worksheets("Ambassadors").Visible = xlSheetVeryHidden
Worksheets("EET").Visible = xlSheetVeryHidden
Worksheets("Strollers").Visible = xlSheetVeryHidden
Worksheets("Dispatchers").Visible = xlSheetVeryHidden
Worksheets("Ambassador Leads").Visible = xlSheetVeryHidden
Worksheets("Tickets").Visible = xlSheetVeryHidden
Worksheets("UBO").Visible = xlSheetVeryHidden
Worksheets("Plaza").Visible = xlSheetVeryHidden
Worksheets("Entry").Visible = xlSheetVeryHidden
Worksheets("Dispatch").Visible = xlSheetVeryHidden
Worksheets("Rentals").Visible = xlSheetVeryHidden
Sheets("CONTROL PANEL").Select




End Sub
 
Upvote 0
.
Try this :

Code:
Sub Disable()
On Error Resume Next
Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Recalc", Schedule:=False
Application.Quit
Workbooks("YourWBNameHere.xlsm").Close SaveChanges:=False
End Sub
 
Upvote 0
This worked perfect man. Took out the last two lines, because I don't want them not being prompted to save, but between your answer and *lightbulb* remembering to call on disable in my workbook close event I have it running perfectly now. Thanks a ton!
 
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,826
Members
449,051
Latest member
excelquestion515

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