Find duplicates in excel

Sam White 6 Reputation points

I have many excel files as below, where I want to find the duplicate values in a column and highlight the entire row with green color. I have the below code, which is only highlighting the duplicate cells in green color. How can I highlight the entire row which has duplicate values in column B.

![$ex = New-Object -ComObject Excel.application  
$ex.visible = $false  
$wb = $ex.Workbooks.Open("C:\Temp\Book2.xlsx")  
$w1= $wb.Worksheets.item(1)  
$r= $w1.Range("B2").EntireColumn  
$dup = $r.FormatConditions.AddUniqueValues()  
$dup.DupeUnique = 1  
$dup.Font.ColorIndex =4  
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,889 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Limitless Technology 37,751 Reputation points

    Hello @Sam White

    I know it may sound very intuitive, but have you tried with .EntireRow property?

    --If the the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  2. Sam White 6 Reputation points

    There is no EntireRow property for $dup, the only properties of $dup are:

    PS C:\WINDOWS\system32> $dup

    Application :
    Creator :
    Parent :
    Priority :
    StopIfTrue :
    AppliesTo :
    DupeUnique :
    Interior :
    Borders :
    Font :
    Type :
    NumberFormat :
    PTCondition :
    ScopeType :

    0 comments No comments

  3. Sam White 6 Reputation points

    Any suggestions on this?

    0 comments No comments

  4. Anthony Axlen 1 Reputation point


    @Sam White - I've achieved this like that:

    • I've created two for loops to go thru the column with duplicated values, compare them (n and n+1) and save row number to array
    • At the end I've used foreach loop to go for all values in array and change the font color:
    • For entire row - $w1.Rows.Item($a).font.colorindex = 3 #red
    • For cell only - $w1.Cells.Item($a,2).font.colorindex = 3 #red

    Thank you.

    0 comments No comments

  5. Sam White 6 Reputation points

    @Anthony Axlen , thanks. Can you please provide the details of the for loop you created.

    0 comments No comments