Robotic Process Automation (RPA)

 View Only
  • 1.  Get duplicate values from an Excel column

    Posted Tue May 28, 2024 12:19 PM

    Hi,

    How can I get duplicate values from a column Excel, for Example, the column looks like this:

    I just need this

    Thanks for your help



    ------------------------------
    Alex Virrueta
    ------------------------------


  • 2.  RE: Get duplicate values from an Excel column

    Posted Wed May 29, 2024 02:27 AM

    Hi,

    I am not aware of a way how to remove duplicates directly with IBM RPA actions (maybe some complicated loop), however, you can run an Office Macro.

        Dim ws As Worksheet
        Set ws = ActiveSheet
        
        ' Define the range where duplicates need to be removed
        Dim rng As Range
        Set rng = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
        
        ' Remove duplicates in the defined range
        rng.RemoveDuplicates Columns:=1, Header:=xlYes ' Change Header to xlNo if there is no header row

    Example of macro(s) is also here: https://stackoverflow.com/questions/61182776/how-do-i-delete-duplicated-rows-in-vba



    ------------------------------
    Milan Babčanec
    ------------------------------



  • 3.  RE: Get duplicate values from an Excel column

    IBM Champion
    Posted 26 days ago

    I agree with Milan that a macro seems like one of the easier ways to implement this. However, the original request was to keep only the duplicates. Below, I've provided a sample macro to accomplish that.

    Sub KeepDuplicates()
    
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    ' Define the range where duplicates need to be found
    Dim rng As Range
    Set rng = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
    
    ' Find and keep only one copy of duplicates
    Dim cell As Range
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    
    For Each cell In rng
        If Not dict.exists(cell.Value) Then
            dict.Add cell.Value, 1
        Else
            dict(cell.Value) = dict(cell.Value) + 1
        End If
    Next cell
    
    ' Remove all but one copy of duplicates
    For Each cell In rng
        If dict(cell.Value) > 1 Then
            dict(cell.Value) = dict(cell.Value) - 1
            cell.ClearContents
        End If
    Next cell
    
    ' Remove empty cells
    rng.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
    
    End Sub


    ------------------------------
    Martin Medina
    ------------------------------