1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| Option Explicit
Declare Function SetTimer Lib "User32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "User32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Const heure_déb As Date = #9:00:00 AM#
Const heure_fin As Date = #12:00:00 PM#
Dim TimerID As Long
Public ArretDemande As Boolean, PremierPassage As Boolean
Sub ArretTimer()
TimerID = KillTimer(Application.hwnd, 0)
DoEvents
If Not TimerID = 0 Then
Debug.Print "Timer stoppé avec succes !"
Else
Debug.Print "erreur arret Timer !"
End If
End Sub
Sub LetsGo()
Dim MonTimerEnSeconde As Long
Debug.Print "Tantative de demarrage à " & Time
TimerID = 0
PremierPassage = True
ArretDemande = False
MonTimerEnSeconde = 10
TimerID = SetTimer(Application.hwnd, 0, MonTimerEnSeconde * 1000, AddressOf MaMacro2)
DoEvents
End Sub
Sub MaMacro2(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
If TimerID = 0 Then Debug.Print "le timer n'a pas demarré !": GoTo AhMince
On Error GoTo AhMince
If Time >= heure_déb And Time < heure_fin Then ' l'heure actuelle est plus grande que l'eure_deb et plus petite que l'heure fin on programe la prochaine minute
If PremierPassage = True Then
Debug.Print "Time >= heure_déb, tout est OK !"
PremierPassage = False
End If
With Sheets("Feuil1")
Debug.Print Time
.Range("E" & .Cells(Rows.Count, "E").End(xlUp).Row + 1).Value = .Cells(8, "F").Value
'une cellule dédiée à l'arret ici "A1" pour l'exemple
If ArretDemande = True Then 'il suffit de selectionner la cellule A1
Debug.Print "arret demandé ok !"
ArretTimer
Exit Sub
End If
End With
DoEvents
Debug.Print "Next..."
Else
If Time >= heure_fin Then Debug.Print "Time >= heure_fin, on arrete tout !"
If Time < heure_déb Then Debug.Print "Time < heure_déb, on arrete tout !"
If Not TimerID = 0 Then ArretTimer
End If
Exit Sub
AhMince:
If Not TimerID = 0 Then ArretTimer
Debug.Print Err.Description
Debug.Print "/!\ Erreur " & Err.Number
End Sub |
Partager