XL 2019 Mise en forme conditionnel, doublon

Yazeubi

XLDnaute Junior
Bonjour j'espère que tout le monde va bien,

Voici ma demande:
J'aimerai à l'aide d'une mise en forme conditionnelle, marquer en rouge les références dans chaque cellules en doublon.
Exemple :

Cellule n°1 :
-A24941A
-A21758A

Cellule n°2 :
-A24941A
-A23589A

La référence A24941A doit apparaître en rouge car elle est présente en double. Toute la cellule ne doit pas être en rouge car toute la cellule n'est pas en double.
J'aimerai que la mise en forme conditionnel soit présente à l'écrit afin que je puisse la copier coller ou la refaire dans d'autre Excel par la suite.

Merci de votre aide.

Yazeubi🍀
 

Pièces jointes

  • Mise en forme conditionnel doublon.xlsm
    16 KB · Affichages: 8

mapomme

XLDnaute Barbatruc
Supporter XLD
Bonjour à tous,

Une macro VBA dans le module de la feuille "Feuil1" Cliquer sur le bouton Hop!

Le code :
VB:
Sub doublons()
Dim der&, dic, t, i&, j&, s, clef, ligne
   Application.ScreenUpdating = False
   If Me.FilterMode Then Me.ShowAllData
   Columns("f").Font.ColorIndex = xlColorIndexAutomatic
   der = Cells(Rows.Count, "f").End(xlUp).Row
   Set dic = CreateObject("scripting.dictionary")
   t = Range("f1:f" & der)
 
   For i = 2 To UBound(t)
      t(i, 1) = Replace(t(i, 1), Chr(10), " ")
      t(i, 1) = Application.Trim(t(i, 1))
      s = Split(t(i, 1))
      For j = 0 To UBound(s)
         If Not dic.exists(s(j)) Then dic.Add s(j), ""
         dic(s(j)) = Trim(dic(s(j)) & " " & i)
      Next j
   Next i
 
   For Each clef In dic
      s = Split(dic(clef))
      If UBound(s) > 0 Then
         For Each ligne In s
            i = InStr(Cells(Val(ligne), "f"), clef)
            Cells(Val(ligne), "f").Characters(Start:=i, Length:=Len(clef)).Font.Color = vbRed
         Next ligne
      End If
   Next clef
End Sub

edit : bonjour @bhbh :)
 

Pièces jointes

  • Yazeubi- colorer doublon- v1.xlsm
    25.6 KB · Affichages: 5
Dernière édition:

Cousinhub

XLDnaute Barbatruc
Bonjour,
Hello @mapomme :)
Bon, comme je l'avais fait..
Un peu différent, même principe (Dictionary)
:
VB:
Sub color_doublon()
Dim Tbl, Ke, Tbl_Sp
Dim Prem_Adr As String
Dim Doub As Object
Dim Cel As Range
Dim I As Long
Dim J As Byte, Pos As Byte
Set Doub = CreateObject("Scripting.Dictionary")
Tbl = Range("F2:F" & Cells(Rows.Count, "F").End(xlUp).Row)
Columns("F:F").Font.ColorIndex = xlAutomatic
For I = LBound(Tbl) To UBound(Tbl)
    If InStr(1, Tbl(I, 1), Chr(10)) Then
        Tbl_Sp = Split(Tbl(I, 1), Chr(10))
        For J = LBound(Tbl_Sp) To UBound(Tbl_Sp)
            If Tbl_Sp(J) <> "" Then Doub(Trim(Tbl_Sp(J))) = Doub(Trim(Tbl_Sp(J))) + 1
        Next J
    Else
        If Tbl(I, 1) <> "" Then Doub(Trim(Tbl(I, 1))) = Doub(Trim(Tbl(I, 1))) + 1
    End If
Next I
For Each Ke In Doub.Keys
    If Doub(Ke) > 1 Then
        Set Cel = Columns(6).Find(Ke)
        If Not Cel Is Nothing Then
            Prem_Adr = Cel.Address
            Pos = InStr(1, Cel.Value, Ke)
            Cel.Characters(Pos, Len(Ke)).Font.ColorIndex = 3
            Cel.Offset(, 1) = "X"
            Do
                Set Cel = Columns(6).FindNext(Cel)
                Pos = InStr(1, Cel.Value, Ke)
                Cel.Characters(Pos, Len(Ke)).Font.ColorIndex = 3
                Cel.Offset(, 1) = "X"
            Loop While Not Cel Is Nothing And Cel.Address <> Prem_Adr
        End If
    End If
Next Ke
End Sub

Bonne journée
 

Yazeubi

XLDnaute Junior
Bonjour,
Hello @mapomme :)
Bon, comme je l'avais fait..
Un peu différent, même principe (Dictionary)
:
VB:
Sub color_doublon()
Dim Tbl, Ke, Tbl_Sp
Dim Prem_Adr As String
Dim Doub As Object
Dim Cel As Range
Dim I As Long
Dim J As Byte, Pos As Byte
Set Doub = CreateObject("Scripting.Dictionary")
Tbl = Range("F2:F" & Cells(Rows.Count, "F").End(xlUp).Row)
Columns("F:F").Font.ColorIndex = xlAutomatic
For I = LBound(Tbl) To UBound(Tbl)
    If InStr(1, Tbl(I, 1), Chr(10)) Then
        Tbl_Sp = Split(Tbl(I, 1), Chr(10))
        For J = LBound(Tbl_Sp) To UBound(Tbl_Sp)
            If Tbl_Sp(J) <> "" Then Doub(Trim(Tbl_Sp(J))) = Doub(Trim(Tbl_Sp(J))) + 1
        Next J
    Else
        If Tbl(I, 1) <> "" Then Doub(Trim(Tbl(I, 1))) = Doub(Trim(Tbl(I, 1))) + 1
    End If
Next I
For Each Ke In Doub.Keys
    If Doub(Ke) > 1 Then
        Set Cel = Columns(6).Find(Ke)
        If Not Cel Is Nothing Then
            Prem_Adr = Cel.Address
            Pos = InStr(1, Cel.Value, Ke)
            Cel.Characters(Pos, Len(Ke)).Font.ColorIndex = 3
            Cel.Offset(, 1) = "X"
            Do
                Set Cel = Columns(6).FindNext(Cel)
                Pos = InStr(1, Cel.Value, Ke)
                Cel.Characters(Pos, Len(Ke)).Font.ColorIndex = 3
                Cel.Offset(, 1) = "X"
            Loop While Not Cel Is Nothing And Cel.Address <> Prem_Adr
        End If
    End If
Next Ke
End Sub

Bonne journée
Bonjour @bhbh,

Cela marche super bien, j'ai une petite demande c'est que la macro se réactualise dès lors que je rentre une nouvelle référence. Ou seconde option quel se réactualise à l'ouverture de l'excel.
Car j'ai remarqué que si je rentre une nouvelle ref qui est en doublon elle ne devient pas rouge.

Merci pour le travail c'est super !
 

Yazeubi

XLDnaute Junior
Bonjour à tous,

Une macro VBA dans le module de la feuille "Feuil1" Cliquer sur le bouton Hop!

Le code :
VB:
Sub doublons()
Dim der&, dic, t, i&, j&, s, clef, ligne
   Application.ScreenUpdating = False
   If Me.FilterMode Then Me.ShowAllData
   Columns("f").Font.ColorIndex = xlColorIndexAutomatic
   der = Cells(Rows.Count, "f").End(xlUp).Row
   Set dic = CreateObject("scripting.dictionary")
   t = Range("f1:f" & der)
 
   For i = 2 To UBound(t)
      t(i, 1) = Replace(t(i, 1), Chr(10), " ")
      t(i, 1) = Application.Trim(t(i, 1))
      s = Split(t(i, 1))
      For j = 0 To UBound(s)
         If Not dic.exists(s(j)) Then dic.Add s(j), ""
         dic(s(j)) = Trim(dic(s(j)) & " " & i)
      Next j
   Next i
 
   For Each clef In dic
      s = Split(dic(clef))
      If UBound(s) > 0 Then
         For Each ligne In s
            i = InStr(Cells(Val(ligne), "f"), clef)
            Cells(Val(ligne), "f").Characters(Start:=i, Length:=Len(clef)).Font.Color = vbRed
         Next ligne
      End If
   Next clef
End Sub

edit : bonjour @bhbh :)
Bonjour @mapomme,

Le travail répond à ma demande c'est vraiment top, le seul point d'amélioration, serait une réactualisation automatique du vba lors de l'ajout de référence au lieu de l'utilisation d'un bouton VBA.
Car j'ai remarqué que si je rentre une nouvelle ref qui est en doublon elle ne devient pas rouge.

Merci pour ton travail.

Bonne soirée.

Yazeubi
 

Discussions similaires

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
312 069
Messages
2 085 040
Membres
102 763
dernier inscrit
NICO26