Consejo: Otros idiomas son traducidos por Google. Puedes visitar el English versión de este enlace.
Iniciar sesión
x
or
x
x
Suscríbete
x

or

¿Cómo resaltar los valores duplicados en diferentes colores en Excel?

doc diferentes colores duplica 1

En Excel, podemos resaltar fácilmente los valores duplicados en una columna con un color usando el Formato condicional, pero, a veces, necesitamos resaltar los valores duplicados en diferentes colores para reconocer los duplicados de manera rápida y fácil como se muestra en la siguiente captura de pantalla. ¿Cómo podría resolver esta tarea en Excel?

Resalte los valores duplicados en una columna con diferentes colores usando el código VBA


flecha azul burbuja derecha Resalte los valores duplicados en una columna con diferentes colores usando el código VBA


De hecho, no hay una manera directa para nosotros de terminar este trabajo en Excel, pero el siguiente código de VBA puede ayudarlo, haga lo siguiente:

1. Seleccione la columna de valores que desea resaltar duplicados con colores diferentes, luego mantenga presionada la tecla ALT + F11 teclas para abrir el Microsoft Visual Basic para aplicaciones ventana.

2. Haz click recuadro > Móduloy pegue el siguiente código en Módulo Ventana.

Código de VBA: Resalte los valores duplicados en diferentes colores:

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. Y luego presione F5 tecla para ejecutar este código, y un cuadro de aviso le recordará que seleccione el rango de datos que desea resaltar los valores duplicados, vea la captura de pantalla:

doc diferentes colores duplica 2

4. Luego haga clic OK botón, todos los valores duplicados se han resaltado en diferentes colores, ver captura de pantalla:

doc diferentes colores duplica 1


Kutools for Excel: la mejor herramienta de productividad de Office aumenta su productividad en un 80%

  • Super Formula Bar (edite fácilmente varias líneas de texto y fórmula); Diseño de lectura (lee y edita fácilmente un gran número de celdas); Pegar en rango filtrado...
  • Combinar celdas / filas / columnas y mantener datos; Contenido de celdas divididas; Combinar filas duplicadas y suma / promedio... Prevenir células duplicadas; Comparar rangos...
  • Seleccione Duplicado o Único Filas; Seleccionar filas en blanco (todas las celdas están vacías); Super Find y Fuzzy Find en muchos libros de trabajo; Selección aleatoria ...
  • Copia exacta Celdas múltiples sin cambiar la referencia de fórmula; Crear referencias automáticamente a múltiples hojas; Insertar viñetas, Casillas de verificación y más ...
  • Fórmulas favoritas e insertadas rápidamente, Gamas, cuadros y cuadros; Cifrar celdas con contraseña Crear una lista de correo y enviar correos electrónicos ...
  • Extracto del texto, Agregar texto, Eliminar por posición, Eliminar espacio; Crear e imprimir subtotales de paginación; Convertir entre contenido de celdas y comentarios...
  • Súper filtro (guardar y aplicar esquemas de filtro a otras hojas); Clasificación avanzada por mes / semana / día, frecuencia y más; Filtro especial por negrita, cursiva ...
  • Combinar libros de trabajo y hojas de trabajo; Combinar tablas basadas en columnas clave; Dividir datos en varias hojas; Conversión por lotes xls, xlsx y PDF...
  • Más de potentes funciones de 300. Funciona con Office 2007-2019 y 365. Soporta todos los idiomas. Fácil despliegue en compañía. Funciones completas de prueba gratuita de 60-day.
pestaña kte 201905

Office Tab lleva la interfaz con pestañas a Office y hace que su trabajo sea mucho más fácil

  • Habilitar la edición y lectura con pestañas en Word, Excel, PowerPoint, Editor, Acceso, Visio y Proyecto.
  • Abra y cree varios documentos en nuevas pestañas de la misma ventana, en lugar de en nuevas ventanas.
  • ¡Aumenta tu productividad en un 50% y reduce cientos de clics de ratón por ti todos los días!
fondo 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 · 15 days ago
    what is the limit of duplicate values for the code to work properly?
  • To post as a guest, your comment is unpublished.
    debbie · 1 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 · 27 days 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 · 1 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 · 27 days 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 · 1 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 · 3 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 · 2 months ago
      Woow man, thanks !!! exactly what I was looking for!
    • To post as a guest, your comment is unpublished.
      skyyang · 3 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 · 6 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 · 3 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 · 7 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 · 6 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 · 7 months ago
    Nvm i got it to work.
  • To post as a guest, your comment is unpublished.
    Bree · 7 months ago
    I can't get it to work it keeps crashing my Excel.
  • To post as a guest, your comment is unpublished.
    Татьяна · 9 months ago
    Огромное СПАСИБО !!!!
  • To post as a guest, your comment is unpublished.
    Prashanth · 9 months 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 · 11 months 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 · 11 months 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 · 11 months 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 · 11 months 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 · 29 days 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