Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, lig As Variant, t As String, col As Variant
Set r = Intersect(Target, Range("C4:C" & Rows.Count), Me.UsedRange)
If Not r Is Nothing Then
With Feuil2 'CodeName de la feuille STAT
lig = Application.Match(CLng(Date), .[A:A], 0)
If IsError(lig) And Application.CountA(r) Then
.[A3:F32].Sort .[A3], xlAscending, Header:=xlNo 'tri de sécurité
If .[A3] < Date - 29 Then .[A3:F3].Delete xlUp 'si plus de 30 jours
lig = .Cells(.Rows.Count, 1).End(xlUp).Row + 1
.Cells(lig, 1) = Date
.Cells(lig, 6) = "=SUM(RC[-4]:RC[-1])"
End If
For Each r In r 'si plusieurs cellules (copier-coller)
If r <> "" Then
t = Left(r(1, 2), InStr(r(1, 2) & " #", " #") - 1)
col = Application.Match(t, .[B2:E2], 0)
If IsNumeric(col) Then
If r <> t Then
MsgBox "Déjà enregistré " & r(1, 2), , r(1, 0)
r = t
End If
Else
col = Application.Match(r, .[A2:E2], 0)
If IsNumeric(col) Then
r(1, 2) = r & " # " & Format(Date, "dd/mm/yyyy")
.Cells(lig, col) = Val(.Cells(lig, col)) + 1
End If
End If
End If
Next
End With
End If
End Sub