Private Sub Worksheet_Change(ByVal Target As Range)
Dim tablo, resu() As String, i As Long, j As Long, x As String, k As Integer
With UsedRange
tablo = .Resize(, 2) 'matrice plus rapide, au moins 2 colonnes
ReDim resu(1 To UBound(tablo), 1 To 1)
For i = 1 To UBound(tablo)
If tablo(i, 1) <> "" Then
For j = i - 1 To 1 Step -1
If tablo(j, 1) <> "" Then
x = resu(j, 1) & "."
k = InStr(x, ".")
Select Case tablo(i, 1)
Case "LOT"
resu(i, 1) = Val(Left(x, k - 1)) + 1
Case "CHAPITRE"
resu(i, 1) = Left(x, k) & Val(Replace(Mid(x, k + 1), ".", "#")) + 1
Case "ARTICLE"
k = InStr(k + 1, x, ".")
resu(i, 1) = Left(x, k) & Val(Mid(x, k + 1)) + 1
End Select
Exit For
End If
Next j
If tablo(i, 1) = "LOT" And resu(i, 1) = "" Then resu(i, 1) = 1 'LOT 1
End If
Next i
'---restitution---
Application.EnableEvents = False
.Columns(1) = tablo
.Columns(2) = resu
Application.EnableEvents = True
End With
End Sub