Running Multiple On time Codes

ajw5173

New Member
Joined
Apr 7, 2016
Messages
45
Hi,

I have a workbook that I want to run 2 Ontime codes. I want the workbook to save every 5 minutes but I also want it to close at 10PM. These codes work independently but I can't figure out how to get them to work together in the ThisWorkbook tab. I would like to have them both be wookbook_open if possible

This is the code:
VBA Code:
Private Sub Workbook_Open()
    On Error Resume Next
 
    Application.OnTime VBA.TimeValue("22:00:00"), "SaveAndCloseWorkBook", , True
End Sub


Private Sub Workbook_WindowActivate(ByVal Wn As Window)
           Application.OnTime Now + TimeValue("00:05:30"), "Save1"
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi

VBA Code:
Public sMacro As String
Public fireTime As Date

Sub startTimer(ByVal idx As Long, ByVal counter As Long, ByVal tick As String, ByVal TimerActive As Boolean)
    fireTime = Now + TimeValue("00:00:05")
    sMacro = "  'pullData " & idx & " , " & counter & ", " & Chr(34) & tick & Chr(34) & ", " & TimerActive & "'"
    Application.OnTime EarliestTime:=fireTime, Procedure:=sMacro, Schedule:=True
End Sub

Sub StopTimer(ByVal idx As Long, ByVal counter As Long, ByVal tick As String, ByVal TimerActive As Boolean)
    Application.OnTime EarliestTime:=fireTime, Procedure:=sMacro, Schedule:=False
End Sub
 
Upvote 0
Thanks! would I place this in the Thiswookbook tab instead of what I have above?

My first macro calls "SaveAndCloseWorkBook" while my second calls "Save1"

Would I still need to reference these codes?
 
Upvote 0
I have a workbook that I want to run 2 Ontime codes. I want the workbook to save every 5 minutes but I also want it to close at 10PM. These codes work independently but I can't figure out how to get them to work together in the ThisWorkbook tab. I would like to have them both be wookbook_open if possible
Try this code.

In the ThisWorkbook module:
VBA Code:
Option Explicit

Private Sub Workbook_Open()
    StartTimer
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    StopTimer
End Sub
In a standard module (e.g. Module1):
Code:
Option Explicit

Public Const SaveInterval As String = "00:05:00"
Public Const CloseTime As String = "22:00:00"

Public NextSaveTime As Date
Public CloseWorkbookTime As Date


Public Sub StartTimer()
    NextSaveTime = Now + TimeValue(SaveInterval)
    Application.OnTime EarliestTime:=NextSaveTime, Procedure:="SaveWorkbook", Schedule:=True
    If CloseWorkbookTime = 0 Then
        If Time < CloseTime Then
            CloseWorkbookTime = TimeValue(CloseTime)
        Else
            CloseWorkbookTime = Date + 1 + TimeValue(CloseTime)
        End If
        Application.OnTime EarliestTime:=CloseWorkbookTime, Procedure:="CloseWorkbook", Schedule:=True
    End If
End Sub

Public Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=NextSaveTime, Procedure:="SaveWorkbook", Schedule:=False
    Application.OnTime EarliestTime:=CloseWorkbookTime, Procedure:="CloseWorkbook", Schedule:=False
    On Error GoTo 0
End Sub

Public Sub SaveWorkbook()
    'Save this workbook and reschedule the timer
    ThisWorkbook.Save
    StartTimer
End Sub

Public Sub CloseWorkbook()
    If Workbooks.Count > 1 Then
        'If more than 1 workbook is open then save and close only this workbook
        ThisWorkbook.Close True
    Else
        'Otherwise, if only 1 workbook is open then close the Excel app, which also closes this workbook
        ThisWorkbook.Save
        Application.Quit
    End If
End Sub
Save, close and reopen the macro workbook to test it.
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,656
Members
449,091
Latest member
peppernaut

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