Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim cell As Range
Dim checkRange As Range
Dim duplicateCell As Range
Dim found As Boolean
Dim originalColor As Long
' Spécifiez la plage de vérification (à partir de B3)
Set checkRange = Me.Range("B3:B" & Me.Cells(Me.Rows.Count, "B").End(xlUp).Row)
Set duplicateCell = Nothing
found = False
' Si la cellule modifiée se trouve dans la plage de vérification
If Not Intersect(Target, checkRange) Is Nothing Then
For Each cell In checkRange
If cell.Value = Target.Value And cell.Address <> Target.Address Then
Set duplicateCell = cell
found = True
Exit For
End If
Next cell
If found Then
MsgBox "Cette donnée est déjà inscrite dans la colonne B à la ligne " & duplicateCell.Row & ".", vbExclamation, "Redondance détectée"
originalColor = duplicateCell.Interior.Color ' Mémoriser la couleur initiale
' Colorer la cellule existante en rouge
duplicateCell.Interior.Color = RGB(255, 0, 0)
End If
End If
End Sub