Macro reseting OnTime on every event

mrMadCat

New Member
Joined
Jun 8, 2016
Messages
39
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hello again.
I need a macro to be started after some time after event of selection change. If the event repeats before the timer has run off - the timer should be reset.
I've made this code but it doesn't reset time. Please help understand what is wrong.
Code:
Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    On Error Resume Next
    Application.OnTime Endtime, "Other_Macro", , False
    Endtime = Now + TimeValue("00:00:05")
    Application.OnTime Endtime, "Other_Macro", , True
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Anyone? Please help. Can't solve this problem. The time doesn't reset and I get Other_Macro executed after every selection change.
 
Upvote 0
You need to store the Endtime value in a static or module level variable ... Something like this :

Code:
[COLOR=#ff0000][B]Private Endtime As Double[/B][/COLOR]

Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    On Error Resume Next
    Application.OnTime Endtime, "Other_Macro", , False
    Endtime = Now + TimeValue("00:00:05")
    Application.OnTime Endtime, "Other_Macro", , True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

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