提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何在Excel中突出顯示不同顏色的重複值?

doc不同的顏色重複1

在Excel中,我們可以用一種顏色輕鬆突出顯示一列中的重複值 條件格式但是,有時候,我們需要突出顯示不同顏色的重複值以快速輕鬆地識別重複項,如下圖所示。 你怎麼能在Excel中解決這個任務?

使用VBA代碼突出顯示具有不同顏色的列中的重複值


箭頭藍色右泡 使用VBA代碼突出顯示具有不同顏色的列中的重複值


事實上,我們沒有直接的方式在Excel中完成這項工作,但是,下面的VBA代碼可能會對您有所幫助,請按照以下步驟操作:

1。 選擇要用不同顏色突出顯示重複項的值列,然後按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2. 點擊 插入 > 模塊,並將以下代碼粘貼到 模塊 窗口。

VBA代碼:突出顯示不同顏色的重複值:

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。 然後按 F5 鍵以運行此代碼,並且提示框會提醒您選擇要突出顯示重複值的數據范圍,請參閱屏幕截圖:

doc不同的顏色重複2

4。 然後點擊 OK 按鈕,所有重複值都以不同顏色突出顯示,請參見屏幕截圖:

doc不同的顏色重複1


Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%

  • 重用: 快速插入 複雜的公式,圖表 以及你以前用過的任何東西; 加密單元格 密碼; 創建郵件列表 並發送電子郵件...
  • 超級方程式酒吧 (輕鬆編輯多行文字和公式); 閱讀佈局 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不丟失數據; 分裂細胞含量; 組合重複的行/列...防止重複的細胞; 比較範圍...
  • 選擇複製或唯一 行; 選擇空行 (所有細胞都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇......
  • 精確複製 多個單元格而不更改公式參考; 自動創建參考 多張表; 插入項目符號,複選框等等......
  • 提取文本,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級過濾器 (將過濾方案保存並應用到其他工作表); 高級排序 按月/週/日,頻率等; 特殊過濾器 用粗體,斜體......
  • 結合工作簿和工作表; 根據鍵列合併表; 將數據拆分為多個表格; 批量轉換xls,xlsx和PDF...
  • 超過300強大的功能。 支持Office / Excel 2007-2019和365。 支持所有語言。 在您的企業或組織中輕鬆部署。 全功能30天免費試用。
kte tab 201905

Office選項卡為Office提供選項卡式界面,使您的工作更輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,Publisher,Access,Visio和Project。
  • 在同一窗口的新選項卡中打開並創建多個文檔,而不是在新窗口中。
  • 通過50%提高您的工作效率,每天為您減少數百次鼠標點擊!
官方底部
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.
    Kelli · 29 days ago
    Whenever I run this code it seems to work for a few seconds (I can see the various colors in the appropriate column...) but when I try to scroll down to see the entire sheet, everything freezes. Eventually I get an error message that Excel is not responding... Any suggestions? My guess is that my spreadsheet is too large? Or I have too many Duplicate Vales and it's throwing Excel for a loop?? i.e. Recent spreadsheet had 567 cells in the column I was attempting to format and there were around 267 duplicates... Too much? Not enough colors in the rainbow, possibly? ;)
  • To post as a guest, your comment is unpublished.
    Douglas · 4 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 · 4 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 · 4 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 · 5 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 · 4 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 · 5 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 · 7 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 · 6 months ago
      Woow man, thanks !!! exactly what I was looking for!
    • To post as a guest, your comment is unpublished.
      skyyang · 6 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 · 9 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 · 6 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 · 10 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 · 9 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 · 10 months ago
    Nvm i got it to work.
  • To post as a guest, your comment is unpublished.
    Bree · 10 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 · 2 years ago
    without empty to change a colour how ????????????????????
    • To post as a guest, your comment is unpublished.
      skyyang · 2 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 · 2 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 · 2 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. · 3 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 · 3 years ago
    It worked for me on a list of part numbers.
    • To post as a guest, your comment is unpublished.
      Alex · 4 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