I am trying to run an infinite left button mouse click in a loop which should be broken when the time reaches specific value (i.e. specific hour). Left click should take place always in the same cell with 5 seconds intervals. I am using Windows 10 and Excel 2016.
I tried to compile some code but the timer function seems to be not working as well as the mouse click does not take place in the same cell. Any suggestions would be appreciated.
Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As LongPtr
Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Timer()
Dim t As Date, tStop As Date
t = Now
tStop = t + TimeValue("19:43:00") 'Adjust the TimeValue as needed "hh:mm:ss"
Do Until t = tStop
DoEvents
SetCursorPos 634, 371 'x and y position
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
Sleep 5000
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
t = Now
Loop
MsgBox "t = " & t & vbCrLf & "tStop = " & tStop
End Sub