Function HasDoublons(Plage As Range, Optional Cells_Empty As Boolean = 0, Optional D_Plage As Boolean = 0)
'********************************************************************************************************************
'Vérifie si dans une plage de cellules contiguës, disposées sur une ligne ou une colonne, il y a au moins un doublon
'********************************************************************************************************************
'- Si doublon(s) --> Nbr de doublons
'- Si pas doublon --> 0
'Paramètre Cells_Empty optionel pour prendre en compte les cellules vides
'Paramètre D_Plage optionel pour renvoyer l'adresse des doublons
Dim Coll As New Collection, cell As Range, Cells_Doublons As Range, Nbr_Empty&
On Error Resume Next
For Each cell In Plage
If cell.Text <> "" Then
Coll.Add "zaza", cell.Text
If Err > 0 And D_Plage Then If Cells_Doublons Is Nothing Then Set Cells_Doublons = cell Else Set Cells_Doublons = Union(Cells_Doublons, cell)
Err.Clear
Else
If Cells_Empty Then
If D_Plage Then If Cells_Doublons Is Nothing Then Set Cells_Doublons = cell Else Set Cells_Doublons = Union(Cells_Doublons, cell)
Nbr_Empty = Nbr_Empty + 1
End If
End If
Next cell
If D_Plage Then
If Cells_Doublons Is Nothing Then HasDoublons = "" Else HasDoublons = Cells_Doublons.Address
Else
HasDoublons = Plage.Count - Coll.Count + Nbr_Empty
End If
End Function
Sub test_doublons()
If Not HasDoublons(Range("B15:B25"), 1, 1) = "" Then Range(HasDoublons(Range("B15:B25"), 1, 1)).Select Else MsgBox "Pas de doublons.", vbOKOnly + vbInformation
End Sub