Astuce: Les autres langues sont Google-Traduction. Vous pouvez visiter le English version de ce lien.
Se connecter
x
or
x
x
S'enregistrer
x

or

Comment mettre en évidence des valeurs en double dans différentes couleurs dans Excel?

doc différentes couleurs duplique 1

Dans Excel, nous pouvons facilement mettre en évidence les valeurs en double dans une colonne avec une couleur en utilisant le Mise en forme conditionnelle, mais, parfois, nous devons mettre en évidence les valeurs en double dans des couleurs différentes pour reconnaître les doublons rapidement et facilement que la capture d'écran suivante. Comment pourriez-vous résoudre cette tâche dans Excel?

Mettez en surbrillance les valeurs dupliquées dans une colonne avec des couleurs différentes en utilisant le code VBA


flèche bleue droite bulle Mettez en surbrillance les valeurs dupliquées dans une colonne avec des couleurs différentes en utilisant le code VBA


En fait, il n'y a aucun moyen direct pour nous de terminer ce travail dans Excel, mais, le code VBA ci-dessous peut vous aider, s'il vous plaît faites comme suit:

1. Sélectionnez la colonne de valeurs que vous voulez mettre en surbrillance avec les couleurs de différence, puis maintenez la touche ALT + F11 clés pour ouvrir le Microsoft Visual Basic pour applications fenêtre.

2. Cliquez insérer > Moduleet collez le code suivant dans le Module Fenêtre.

Code VBA: Mettez en surbrillance les valeurs dupliquées dans différentes couleurs:

Sub ColorCompanyDuplicates()
'Updateby Extendoffice 20160704
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim xChar As String
    Dim xCellPre As Range
    Dim xCIndex As Long
    Dim xCol As Collection
    Dim I As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    xCIndex = 2
    Set xCol = New Collection
    For Each xCell In xRg
      On Error Resume Next
      xCol.Add xCell, xCell.Text
      If Err.Number = 457 Then
        xCIndex = xCIndex + 1
        Set xCellPre = xCol(xCell.Text)
        If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
        xCell.Interior.ColorIndex = xCellPre.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
        MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
        Exit Sub
      End If
      On Error GoTo 0
    Next
End Sub

3. Et puis appuyez sur F5 clé pour exécuter ce code, et une boîte de dialogue vous rappellera de sélectionner la plage de données que vous souhaitez mettre en évidence les valeurs en double, voir capture d'écran:

doc différentes couleurs duplique 2

4. Puis clique OK bouton, toutes les valeurs en double ont été mises en surbrillance dans différentes couleurs, voir capture d'écran:

doc différentes couleurs duplique 1


Kutools for Excel résout la plupart de vos problèmes et augmente votre productivité de 80%

  • Réutilisation: Insérer rapidement formules complexes, graphiques et tout ce que vous avez utilisé auparavant; Crypter les cellules avec mot de passe Créer une liste de diffusion et envoyer des emails ...
  • Super Formula Bar (éditez facilement plusieurs lignes de texte et de formule); Disposition de lecture (facilement lire et éditer un grand nombre de cellules); Coller à la gamme filtrée...
  • Fusionner les cellules / rangées / colonnes sans perdre de données; Contenu des cellules divisées; Combiner les lignes / colonnes en double... Prévenir les cellules en double; Comparer les plages...
  • Sélectionnez Dupliquer ou Unique Des rangées; Sélectionnez les lignes vierges (toutes les cellules sont vides); Super Find et Fuzzy Find dans de nombreux cahiers d'exercices; Sélection aléatoire ...
  • Copie exacte Plusieurs cellules sans changer la référence de la formule; Créer automatiquement des références à plusieurs feuilles; Insérer des balles, Cases à cocher et plus ...
  • Extrait du texte, Ajouter du texte, Supprimer par position, Supprimer l'espace; Créer et imprimer des sous-totaux de pagination; Conversion entre contenu de cellules et commentaires...
  • Super filtre (enregistrer et appliquer des schémas de filtrage à d'autres feuilles); Tri avancé par mois / semaine / jour, fréquence et plus; Filtre spécial en gras, en italique ...
  • Combinaison de classeurs et de feuilles de calcul; Fusionner les tables en fonction des colonnes clés; Fractionner les données en plusieurs feuilles; Conversion par lots xls, xlsx et PDF...
  • Plus que de puissantes fonctionnalités 300. Prend en charge Office / Excel 2007-2019 et 365. Prend en charge toutes les langues. Déploiement facile dans votre entreprise ou organisation. Fonctionnalités complètes Essai gratuit du jour 30.
kte tab 201905

Office Tab apporte une interface à onglets à Office et simplifie grandement votre travail

  • Activer l'édition par onglets et la lecture dans Word, Excel, PowerPoint, Publisher, Access, Visio et Project.
  • Ouvrez et créez plusieurs documents dans de nouveaux onglets de la même fenêtre, plutôt que dans de nouvelles fenêtres.
  • Augmente votre productivité de 50% et réduit le nombre de clics de souris pour vous chaque jour!
fond officetab
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Douglas · 3 months ago
    what is the limit of duplicate values for the code to work properly?
  • To post as a guest, your comment is unpublished.
    debbie · 3 months ago
    I'm a beginner (obviously), but I keep getting the following error "Compile Error: Invalid outside procedure." Any suggestions?
    • To post as a guest, your comment is unpublished.
      skyyang · 3 months ago
      Hello, debbie,
      The code works well in my worksheet, which Excel version do you use?
      Or you can insert your problem screenshot here.
  • To post as a guest, your comment is unpublished.
    Peter · 4 months ago
    Hallo and thx for your work - know its long time ago but still great to use

    I use it in a large address list and filter out all double entry’s - help a lot

    Now my question - is it possible to run this macro in some kind of live error check - I mean - if someone enter a doable address it get colored directly when I press enter.


    Sub ColorCompanyDuplicates()
    'Updateby Extendoffice 20171222
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim xChar As String
    Dim xCellPre As Range
    Dim xCIndex As Long
    Dim xCol As Collection
    Dim I As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
    xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
    xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Range("M10:P10010")
    If xRg Is Nothing Then Exit Sub
    xCIndex = 2
    Set xCol = New Collection
    For Each xCell In xRg
    On Error Resume Next
    If xCell.Value <> "" Then
    xCol.Add xCell, xCell.Text
    If Err.Number = 457 Then
    xCIndex = xCIndex + 1
    Set xCellPre = xCol(xCell.Text)
    If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.Color = RGB(xRed, xGreen, xBlue)
    xCell.Interior.Color = xCellPre.Interior.Color
    ElseIf Err.Number = 9 Then
    MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
    Exit Sub
    End If
    xRed = Application.WorksheetFunction.RandBetween(0, 255)
    xGreen = Application.WorksheetFunction.RandBetween(0, 255)
    xBlue = Application.WorksheetFunction.RandBetween(0, 255)
    On Error GoTo 0
    End If
    Next
    End Sub


    Thx for your help
    • To post as a guest, your comment is unpublished.
      skyyang · 3 months ago
      Hi, Peter,
      Sorry, maybe there is no direct code for coloring the cells dynamically when entering new data.
      If others have any good ideas, please comment here.
      Thank you!
  • To post as a guest, your comment is unpublished.
    alex · 4 months ago
    I have 1039 rows some have duplicate names but when I run the code it seems to only highlight 100 of the rows and in between it misses some. Is the problem not to many colors? or is it too much to process? (there might be like 500-800 names that repeat)
  • To post as a guest, your comment is unpublished.
    Thomas · 6 months ago
    Because you are limited to 56 Colours using the *.ColorIndex you should probably consider using RGB colours.

    I have modified the code (note there is no if-statement for duplicate coloruing)…

    Sub ColorCompanyDuplicates()
    'Updateby Extendoffice 20160704
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim xChar As String
    Dim xCellPre As Range
    Dim xCIndex As Long
    Dim xCol As Collection
    Dim I As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
    xTxt = ActiveWindow.RangeSelection.AddressLocal
    Else
    xTxt = ActiveSheet.UsedRange.AddressLocal
    End If
    Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    xCIndex = 2
    Set xCol = New Collection
    For Each xCell In xRg
    On Error Resume Next
    xCol.Add xCell, xCell.Text
    If Err.Number = 457 Then
    xCIndex = xCIndex + 1
    Set xCellPre = xCol(xCell.Text)
    If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.Color = RGB(xRed, xGreen, xBlue)
    xCell.Interior.Color = xCellPre.Interior.Color
    ElseIf Err.Number = 9 Then
    MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
    Exit Sub
    End If
    xRed = Application.WorksheetFunction.RandBetween(0, 255)
    xGreen = Application.WorksheetFunction.RandBetween(0, 255)
    xBlue = Application.WorksheetFunction.RandBetween(0, 255)
    On Error GoTo 0
    Next
    End Sub
    • To post as a guest, your comment is unpublished.
      javier · 5 months ago
      Woow man, thanks !!! exactly what I was looking for!
    • To post as a guest, your comment is unpublished.
      skyyang · 5 months ago
      Dear, Thomas,
      Thanks a lot for your code, your are a warm-hearted man, the code may help for others!
      Thanks again!
  • To post as a guest, your comment is unpublished.
    AW · 8 months ago
    How can you make it color the entire document if there are many rows?
    • To post as a guest, your comment is unpublished.
      skyyang · 5 months ago
      Hi, AW,
      To highlight entire documnet based on the duplicate cell values, you can apply the following VBA code:

      Sub ColorCompanyDuplicates()
      Dim xRg As Range
      Dim xTxt As String
      Dim xCell As Range
      Dim xChar As String
      Dim xCellPre As Range
      Dim xCIndex As Long
      Dim xCol As Collection
      Dim I As Long
      On Error Resume Next
      If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
      Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
      End If
      Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
      If xRg Is Nothing Then Exit Sub
      xCIndex = 2
      Set xCol = New Collection
      For Each xCell In xRg
      On Error Resume Next
      xCol.Add xCell, xCell.Text
      If Err.Number = 457 Then
      xCIndex = xCIndex + 1
      Set xCellPre = xCol(xCell.Text)
      If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.EntireRow.Interior.ColorIndex = xCIndex
      xCell.EntireRow.Interior.ColorIndex = xCellPre.EntireRow.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
      MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
      Exit Sub
      End If
      On Error GoTo 0
      Next
      End Sub

      Please try it, hope it can help you!
  • To post as a guest, your comment is unpublished.
    Miles · 9 months ago
    Thanks for posting this, it's been very helpful! Which lines in the VBA code designate which colors will be used? I was looking for hex color codes in there but nothing was popping out.
    • To post as a guest, your comment is unpublished.
      skyyang · 8 months ago
      Hi, Miles,
      The above VBA code can not support to choose the colors, it fills the cells with random background color.
      If you have other good ideas, please comment here.
      Thank you!
  • To post as a guest, your comment is unpublished.
    Bree · 9 months ago
    Nvm i got it to work.
  • To post as a guest, your comment is unpublished.
    Bree · 9 months ago
    I can't get it to work it keeps crashing my Excel.
  • To post as a guest, your comment is unpublished.
    Татьяна · 1 years ago
    Огромное СПАСИБО !!!!
  • To post as a guest, your comment is unpublished.
    Prashanth · 1 years ago
    Oh Damn, You saved my time,,I was taking help of my colleagues all these days and no one had idea about it. Thanks a Ton !!!
  • To post as a guest, your comment is unpublished.
    Carol · 1 years ago
    My spreadsheet also stopped coloring at 178 and I have over 400 lines. How do you fix this?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, Carol,
      Could you send your workbook to my email address, I may help you to find the problem.
      My email address is :skyyang@extendoffice.com
  • To post as a guest, your comment is unpublished.
    hidayat_wijaya · 1 years ago
    This is really great, but colouring stopped after row 76 (5 colours). How can I this be extended too?
  • To post as a guest, your comment is unpublished.
    Anri · 1 years ago
    This is really great, but colouring stopped after row 66 (9 colours). How can I this be extended?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, Anri,
      The above code works well in my worksheet, i test it in 300 hundred rows.
      Please try it again. Or you can send your workbook file to my email account.
      My email account is: skyyang@extendoffice.com
      • To post as a guest, your comment is unpublished.
        Ahmed · 1 years ago
        Hello,
        my excel sheet has 11000 row of data.
        how can I extend it to highlight all the duplicate in that long column.

        it stopped at row 77.

        Thanks,

        AK
      • To post as a guest, your comment is unpublished.
        Bruce luo · 1 years ago
        there is some mistake regarding the colorindex setting, xCindex will be more than 56 if there are 56 row data in your sheet, system will ignore the sentence :
        If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
        xCell.Interior.ColorIndex = xCellPre.Interior.ColorIndex
        I correct the program like below: \
        if Err.number=457 then
        if xCellPre.Text<>xCell.Text Then
        xCindex=xCindex+1
        endif
        set.....
  • To post as a guest, your comment is unpublished.
    Priya · 1 years ago
    I am new to VBA. Is there any way, that we need not run the macro over and over, it is automated to highlight even if new cells are copied into the column where macro is programmed?
  • To post as a guest, your comment is unpublished.
    Vasil · 1 years ago
    Is there a way to change the script to work for (look at) table array instead of column? For example F2:BC117.
    Thank you!
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, Vasil,
      To highlight duplicate values in a range of cell, please try the following vba code:

      Sub ColorCompanyDuplicates()
      'Updateby Extendoffice
      Dim xRg, xRgRow As Range
      Dim xTxt, xStr As String
      Dim xCell, xCellPre As Range
      Dim xCIndex As Long
      Dim xCol As Collection
      Dim I As Long
      If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
      Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
      End If
      Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
      If xRg Is Nothing Then Exit Sub
      xCIndex = 2
      Set xCol = New Collection
      For I = 1 To xRg.Rows.Count
      On Error Resume Next
      Set xRgRow = xRg.Rows(I)
      For Each xCell In xRgRow.Columns
      xStr = xStr & xCell.Text
      Next
      xCol.Add xRgRow, xStr
      If Err.Number = 457 Then
      xCIndex = xCIndex + 1
      Set xCellPre = xCol(xStr)
      If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
      xRgRow.Interior.ColorIndex = xCellPre.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
      MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
      Exit Sub
      End If
      On Error GoTo 0
      xStr = ""
      Next
      End Sub

      Hope it can help you.
  • To post as a guest, your comment is unpublished.
    Bobo · 1 years ago
    Is there a way to highlight the entire row instead of 1 column?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hi, Bobo,
      To highlight entire row based on the duplicate cell values, you can apply the following VBA code:

      Sub ColorCompanyDuplicates()
      Dim xRg As Range
      Dim xTxt As String
      Dim xCell As Range
      Dim xChar As String
      Dim xCellPre As Range
      Dim xCIndex As Long
      Dim xCol As Collection
      Dim I As Long
      On Error Resume Next
      If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
      Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
      End If
      Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
      If xRg Is Nothing Then Exit Sub
      xCIndex = 2
      Set xCol = New Collection
      For Each xCell In xRg
      On Error Resume Next
      xCol.Add xCell, xCell.Text
      If Err.Number = 457 Then
      xCIndex = xCIndex + 1
      Set xCellPre = xCol(xCell.Text)
      If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.EntireRow.Interior.ColorIndex = xCIndex
      xCell.EntireRow.Interior.ColorIndex = xCellPre.EntireRow.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
      MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
      Exit Sub
      End If
      On Error GoTo 0
      Next
      End Sub

      Please try it, hope it can help you!
      • To post as a guest, your comment is unpublished.
        Hossein · 1 years ago
        how can I highlight the range of rows?
        • To post as a guest, your comment is unpublished.
          skyyang · 1 years ago
          Hello, Hossein,
          May be the following code can do you a favor, please try it.

          Sub ColorCompanyDuplicates()
          'Updateby Extendoffice
          Dim xRg, xRgRow As Range
          Dim xTxt, xStr As String
          Dim xCell, xCellPre As Range
          Dim xCIndex As Long
          Dim xCol As Collection
          Dim I As Long
          If ActiveWindow.RangeSelection.Count > 1 Then
          xTxt = ActiveWindow.RangeSelection.AddressLocal
          Else
          xTxt = ActiveSheet.UsedRange.AddressLocal
          End If
          Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
          If xRg Is Nothing Then Exit Sub
          xCIndex = 2
          Set xCol = New Collection
          For I = 1 To xRg.Rows.Count
          On Error Resume Next
          Set xRgRow = xRg.Rows(I)
          For Each xCell In xRgRow.Columns
          xStr = xStr & xCell.Text
          Next
          xCol.Add xRgRow, xStr
          If Err.Number = 457 Then
          xCIndex = xCIndex + 1
          Set xCellPre = xCol(xStr)
          If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
          xRgRow.Interior.ColorIndex = xCellPre.Interior.ColorIndex
          ElseIf Err.Number = 9 Then
          MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
          Exit Sub
          End If
          On Error GoTo 0
          xStr = ""
          Next
          End Sub
  • To post as a guest, your comment is unpublished.
    selim · 1 years ago
    What If I just want to fill with only two colors, let's say yellow and red, repeatedly. To be clear, on the example in this page, 'Rachel' is yellow, Rose is red and again Sussies are yellow, Tedi is red.
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, selim,
      The following code may solve your problem, please try.

      Sub ColorCompanyDuplicates()
      'Updateby Extendoffice 20170504
      Dim xRg As Range
      Dim xTxt As String
      Dim xCell As Range
      Dim xChar As String
      Dim xCellPre As Range
      Dim xRgTemp As Range
      Dim xCIndex As Long
      Dim xCol As Collection
      Dim I As Long
      On Error Resume Next
      If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
      Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
      End If
      Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
      If xRg Is Nothing Then Exit Sub
      xCIndex = 3
      Set xCol = New Collection
      For Each xCell In xRg
      On Error Resume Next
      xCol.Add xCell, xCell.Text
      If Err.Number = 457 Then
      Set xCellPre = xCol(xCell.Text)
      xCell.Interior.ColorIndex = xCellPre.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
      MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
      Exit Sub
      Else
      xCell.Interior.ColorIndex = xCIndex
      Set xRgTemp = xCell
      xCIndex = IIf(xRgTemp.Interior.ColorIndex = 3, 4, 3)
      End If
      On Error GoTo 0
      Next
      End Sub

      Hope it can help you!
      • To post as a guest, your comment is unpublished.
        selim · 1 years ago
        This is what I exactly want it. Thank you much, skyyang.
  • To post as a guest, your comment is unpublished.
    Asking Man · 1 years ago
    Hello, I have Excel 2016, alt+F11 does work anymore to bring up Microsoft VB? is Microsoft visual basic free software? Thank you.
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hi,
      If you can not activate the Microsoft VB window by holding down Alt + F11 keys, you can click Developer > Visual Basic to open it.

      Please try it, thank you!
  • To post as a guest, your comment is unpublished.
    shahinshah · 1 years ago
    Sir,
    How to differentiate different colors given in the data on the basis of frequency?
    In very large data same color has been given repeatedly without considering their frequency.
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Sorry, can you give more detailed information,you can attach a screenshot here.
      Thank you!
  • To post as a guest, your comment is unpublished.
    gopi · 1 years ago
    without empty to change a colour how ????????????????????
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, gopi,
      To avoid the blank cells, please apply the following VBA code:
      Sub ColorCompanyDuplicates()
      'Updateby Extendoffice 20171222
      Dim xRg As Range
      Dim xTxt As String
      Dim xCell As Range
      Dim xChar As String
      Dim xCellPre As Range
      Dim xCIndex As Long
      Dim xCol As Collection
      Dim I As Long
      On Error Resume Next
      If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.AddressLocal
      Else
      xTxt = ActiveSheet.UsedRange.AddressLocal
      End If
      Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
      If xRg Is Nothing Then Exit Sub
      xCIndex = 2
      Set xCol = New Collection
      For Each xCell In xRg
      On Error Resume Next
      If xCell.Value <> "" Then
      xCol.Add xCell, xCell.Text
      If Err.Number = 457 Then
      xCIndex = xCIndex + 1
      Set xCellPre = xCol(xCell.Text)
      If xCellPre.Interior.ColorIndex = xlNone Then xCellPre.Interior.ColorIndex = xCIndex
      xCell.Interior.ColorIndex = xCellPre.Interior.ColorIndex
      ElseIf Err.Number = 9 Then
      MsgBox "Too many duplicate companies!", vbCritical, "Kutools for Excel"
      Exit Sub
      End If
      On Error GoTo 0
      End If
      Next
      End Sub

      Hope it can help you, thank you!
  • To post as a guest, your comment is unpublished.
    bhaggi · 1 years ago
    how to change colour ?
    • To post as a guest, your comment is unpublished.
      Edmund Pelingon · 1 years ago
      the same problem with me... color is too dark to read...
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hi,
      The code only can help you add the different color randomly, it can't change the color.
      Thank you!
      • To post as a guest, your comment is unpublished.
        John · 1 years ago
        It seems to always use the same color palette though, is there a way to select the palette it uses? It's giving me some really dark colors through which the text is unreadable.
  • To post as a guest, your comment is unpublished.
    SHRIKANT NAYAK · 2 years ago
    I am really happy as I got what I was needed. Thanks
  • To post as a guest, your comment is unpublished.
    Sarah · 2 years ago
    This is great and EXACTLY what I was looking for! I'm incorporating this code into some existing code - I've written my code to select the cells that I want to color, and then I call the code to do the coloring. The only thing I can't figure out is how to bypass the msgBox that pops up and I have to click OK. I'm a novice at VBA and can't quite figure out how to alter this code.... Any suggestions, please! :)
    • To post as a guest, your comment is unpublished.
      Wojciech Radwan · 1 years ago
      Replace line: Set xRg = Application.InputBox("please select the data range:", "Kutools for Excel", xTxt, , , , , 8)
      to
      Set xRg = Range("A1:A100")

      or if you have table you can apply to whole table column:
      Set xRg = Range("Table1[[#All],[Column1]]")

      just replace Table1 to your own name and Column1 to any table header you wish to apply this macro.


      Regards
      Wojciech
  • To post as a guest, your comment is unpublished.
    Jason · 2 years ago
    I tried running this several times and every time I click "ok" it just sends me back to the modules screen. I'm using Excel 2010.
  • To post as a guest, your comment is unpublished.
    Josh · 2 years ago
    I had the same problem, the problem is the color index only goes to 56, so once it passes that it no longer colors the cells. To fix that, I replaced the line "xCIndex = xCIndex + 1" with the following:

    If xCIndex > 55 Then
    xCIndex = 3
    Else
    xCIndex = xCIndex + 1
    End If

    It will start reusing colors eventually, but that wasn't an issue for me.
    • To post as a guest, your comment is unpublished.
      Golzar · 1 years ago
      Thank you so much Josh, it works!
    • To post as a guest, your comment is unpublished.
      FRANK · 2 years ago
      The replace with

      If xCIndex > 55 Then
      xCIndex = 3
      Else
      xCIndex = xCIndex + 1
      End If



      Did not work. Trying to get this to work on 14000 lines, approx 6000 duplicates
      • To post as a guest, your comment is unpublished.
        Golzar · 1 years ago
        It worked for me, I indented the second and fourth lines. See below. Josh's code is bolded.

        If Err.Number = 457 Then
        If xCIndex > 55 Then
        xCIndex = 3
        Else
        xCIndex = xCIndex + 1
        End If
        Set xCellPre = xCol(xCell.Text)
  • To post as a guest, your comment is unpublished.
    ES · 2 years ago
    Same problem. Anyone figure this out?
  • To post as a guest, your comment is unpublished.
    Golzar · 2 years ago
    This has been a life saver for me, thank you so much for sharing!

    When I run it on about 2000 cells with values, it only highlights some of the duplicates. Is there a way to fix that? I wonder if it runs out of colors or there is something else.
    • To post as a guest, your comment is unpublished.
      yp · 2 years ago
      same problem i am trying with couple of hundred cells and very quickly it colors in same colors.
      is there a fix for this?
      thanks
  • To post as a guest, your comment is unpublished.
    Swapnil · 2 years ago
    i want to check the duplicates for 5000 cells which i am not able to do. i can highlight duplicates upto 70 to 80 cells
  • To post as a guest, your comment is unpublished.
    Patrick A. · 2 years ago
    This is just what I needed, thank you.

    Sometimes when I run this code Excel just freezes, I am using Office 2016 / Windows 10 any idea why?
    • To post as a guest, your comment is unpublished.
      Alan · 2 years ago
      Patrick, only highlight the cells you want. Don't highlight the entire column which will include all the thousands of blank cells
  • To post as a guest, your comment is unpublished.
    Edward · 2 years ago
    It worked for me on a list of part numbers.
    • To post as a guest, your comment is unpublished.
      Alex · 3 months ago
      Hello,

      Is there a way to make this only effect the highlighted column and not the entire row? Some of the bold red and blue colors are hard to look at all the way across the spreadsheet. Thanks