Public t# 'mémorise la variable
Sub Enregistrer()
Dim chemin$, fichier$, a(), n&
chemin = ThisWorkbook.Path & "\"
ThisWorkbook.SaveAs chemin & "enregistrement " & Format(Now, "dd-mm-yy hh-mm-ss")
On Error Resume Next
Application.OnTime t, "Enregistrer", , False
t = Now + 5 / 1440 'délai de 5 minutes
Application.OnTime t, "Enregistrer"
fichier = Dir(chemin & "enregistrement*.xlsm")
While fichier <> ""
ReDim Preserve a(n) 'base 0
a(n) = CDate(Mid(fichier, 16, 9) & Replace(Mid(fichier, 25, 8), "-", ":"))
If a(n) <> "" Then n = n + 1
fichier = Dir
Wend
tri a, 0, UBound(a)
'---on ne garde que les 2 derniers fichiers---
For n = 0 To UBound(a) - 2
Kill chemin & "enregistrement " & Format(a(n), "dd-mm-yy hh-mm-ss") & ".xlsm"
Next
End Sub
Sub tri(a, gauc, droi) ' Quick sort
Dim ref, g, d, temp
ref = a((gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While a(g) < ref: g = g + 1: Loop
Do While ref < a(d): d = d - 1: Loop
If g <= d Then
temp = a(g): a(g) = a(d): a(d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call tri(a, g, droi)
If gauc < d Then Call tri(a, gauc, d)
End Sub