VBA End Application.Ontime Macro

Fobling

New Member
Joined
Jun 18, 2018
Messages
1
Morning all,

I'm not a VBA pro, so you can imagine my delight when I managed to auto start a macro, that made the test in one of my cells "blink". However, the file now opens everytime I close it - because I haven't ended my Application.OnTime and I just can't get my head round it.

So, in my "ThisWorkbook" my code looks like:
Code:
Private Sub workbook_Open()
Call StartBlink
End Sub
And in my module, the corresponding code looks like:
Code:
Sub StartBlink()
    Dim xCell As Range
    Dim xTime As Variant
    Set xCell = ThisWorkbook.Worksheets("VOC -1").Range("M5")
    With ThisWorkbook.Worksheets("VOC -1").Range("M5").Font
        If xCell.Font.Color = vbRed Then
            xCell.Font.Color = vbWhite
        Else
            xCell.Font.Color = vbRed
        End If
    End With
    xTime = Now + TimeSerial(0, 0, 0.75)
    Application.OnTime xTime, "'" & ThisWorkbook.Name & "'!StartBlink", , True


End Sub

How do I make it stop when I close the file?

Thanks,

Fi
 
Last edited by a moderator:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Add the following to the ThisWorkbook object:

Rich (BB code):
Private Sub Workbook_BeforeClose()
    Call StopBlink
End Sub

And make the following changes in the module that constains StartBlink.

Rich (BB code):
Dim xTime As Variant

Sub StartBlink()
    Dim xCell As Range
    [.... remove Dim xTime ....]
  
    [....]
    xTime = Now + TimeSerial(0, 0, 0.75)
    Application.OnTime xTime, "'" & ThisWorkbook.Name & "'!StartBlink", , True
End Sub

Sub StopBlink()
    On Error Resume Next
    Application.OnTime xTime, "StartBlink",, False
End Sub

It should not be necessary to prefix the sub name (StartBlink) with the workbook name.

Also, I believe that TimeSerial(0,0,0.75) is rounded to TimeSerial(0,0,1). Use 0.75/86400 instead.

But note that Now() is truncated to the current second. So the first OnTime event might occur sooner than expected.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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