Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Timer Excel VBA

0

Dear All,

I am not an expert on VBA excel.

I have been trying to di this:

1. Run a timer every time excel is open.

2. Close excel after that timer ends (go to zero).

I attached an excel file with the code.

Don't know why but there are two issues, one is every time I do something on excel for example click on other cell then the timer re-start,the other issue is after excel is closed it opens again automatically. If I close excel before timer go to zero it seems ok

Can someone help me with these issues or even giving me other codes doing what I would like, it would be great. Thanks for any help.

Answer
Discuss

Discussion

Hi FREX_420

Thanks for asking a fresh question (as requested)- I'll try to reply tomorrow or over the weekend. Please be sure to check back here for an answer.
John_Ru (rep: 6142) Jan 26, '23 at 4:10 pm
Add to Discussion

Answers

0
Selected Answer

Hi FREX_420

The application Timer operates on an Earliest Time basis and so can be delayed if you do something in Excel.

Your file also resets the 15 second timer when there's an entry made of new sheet picked (since the Workbook_SheetCalculate or Workbook_SheetSelectionChange event macros stop and reset that timer).

If you need a countdown timer and want the file to close after 15 seconds (or as close as possible to that, given activity), please try the revised file attached.

At a workbook level, it just sets cell S2 and starts the seconds countdown timer using this:

Private Sub Workbook_Open()
    Call SetTimer
    Call Timer
End Sub

with the event macros disabled (commented out).

On your modules, my chnages are (with comments):

Option Explicit

Public DownTime As Date

Sub SetTimer()
    ' Just set the time and allow Timer sub do the work
    Range("S2").Value = "00:00:15"
    DownTime = Now + TimeValue("00:00:15")
'   ### lose this line
'    Application.OnTime EarliestTime:=DownTime, Procedure:="ShutDown", Schedule:=True
End Sub

' ### lose this 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

and the other module:

Option Explicit

Dim Interval As Date

Sub Timer()
    ' set timer value (one second)
    Interval = Now + TimeValue("00:00:01")
    If Worksheets("Sheet1").Range("S2").Value <= 0 Then
        ' reached 0 with no running timer (so just need to quit)
        Call ShutDown
        Else
        ' if not 0, decrement S2
        Worksheets("Sheet1").Range("S2") = DownTime - Now
        Application.OnTime EarliestTime:=Interval, Procedure:="Timer"
    End If
End Sub

Sub Stop_Timer()
     Application.OnTime EarliestTime:=Interval, Procedure:="Timer", Schedule:=False
End Sub

If you don't need the countdown, you could instead use just the 15 second timer and its Stop in a similar way- that would probably lead to an earlier closure since as soon as an activity stops, the expired timer would lead to the file being closed.

Hope this fixes things for you. If so, please rember to mark this Answer as Selected.

Discuss

Discussion

Hi John,

That worked perfectely!!

Thank you so much not only because you made It possible but also because you were so kind writing a great explanation.

Great dedication.

Many thanks :)
ALL the best.
Frex.
FREX_420 (rep: 6) Jan 28, '23 at 6:38 am
FREX. Glad that worked but, as I said "If so, please rember to mark this Answer as Selected" - this increases both our reputations in the Forum (and is my only reward, apart from your thanks).
John_Ru (rep: 6142) Jan 28, '23 at 7:08 am
Hi John,
I really did that First time when tou asked me for. I think I have It done now again. So Sorry, can tou confirm It is ok now? I don,t find anymore the blue word to select.please let me know. This is all new to me.
Frex.
FREX_420 (rep: 6) Jan 28, '23 at 1:01 pm
Thanks Frex, the Answer now shows as Selected. I realise it's all new to you / your first time asking a question but hopefully you got what you wanted. Enjoy your weekend!
John_Ru (rep: 6142) Jan 28, '23 at 1:32 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login