Timer to switch sheets at different times

djmurray

New Member
Joined
Jan 8, 2013
Messages
30
Good day,

I have a dashboard that I have built within excel, however due to the amount of data that we require it to display, I have had to make the sheets switch.

I was using the below to switch the sheets every minute, however the sheet "Specials" doesn't need to display for the same amount of time as the overview.

I would like to make the specials page show for 15 seconds and the overview to show for 2 minutes but I just cannot get my head around the code.

Please see below.


Code:
Public RunWhen As Double
Public Const cRunIntervalSeconds = 60 ' 1 Minute
Public Const cRunWhat = "SwitchSheets"


Sub StartAutoSwitch()
    RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
        Schedule:=True
End Sub


Sub SwitchSheets()
If ActiveSheet.Name = "Specials" Then
    Sheets("Overview").Activate
Else
    Sheets("Specials").Activate
End If
StartAutoSwitch
End Sub


Sub StopTimer()
    On Error Resume Next
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, _
        Schedule:=False
        
MsgBox ("AutoUpdate Has Been Stopped")
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi, I think this should work:
Code:
Private RunWhen As Double
Private Const cRunWhat = "SwitchSheets"

Sub StartAutoSwitch(Seconds As Long)
    RunWhen = Now + TimeSerial(0, 0, Seconds)
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=True
End Sub


Sub SwitchSheets()
    If ActiveSheet.Name = "Specials" Then
        Sheets("Overview").Activate
        StartAutoSwitch (120)
    Else
        Sheets("Specials").Activate
        StartAutoSwitch (15)
    End If
End Sub


Sub StopTimer()
    'On Error Resume Next
    Application.OnTime EarliestTime:=RunWhen, Procedure:=cRunWhat, Schedule:=False
    MsgBox ("AutoUpdate Has Been Stopped")
End Sub

I have changed Public into Private at the start. As long as the macros are in the same Module then Private should be good enough.

I also changed StartAutoSwitch to accept a time period so it can be set depending on the sheet activated.

Regards,
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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