Friday 14 August 2015

How to remove duplicate rows using VBA code?

I have excel sheet with data up to 5k rows. What I need to do is to remove duplicate rows having similar value at Column A and C. The problem now I need to combine data at Column F before I can delete duplicate rows. By using formula I don’t think it will work. Therefore I have to write Macro to perform this work. The code that work as below. Hope this code will help who ever have same problem.

<pre style="overflow: auto; width: 94%;">
Option Explicit
Sub RemoveDuplicateAndBringUpCommon()
    Dim x As Long, y As Long
    Dim ActWs As Worksheet
    Set ActWs = ActiveSheet
    x = 2
    y = x + 1
    Do While ActWs.Range("C" & x).Value <> ""
        Do While ActWs.Range("C" & y).Value <> ""
            If ActWs.Range("A" & x).Value = ActWs.Range("A" & y).Value And _
            ActWs.Range("C" & x).Value = ActWs.Range("C" & y) Then
                ActWs.Range("F" & x) = ActWs.Range("F" & x) & "," & ActWs.Range("F" & y)
                ActWs.Rows(y).EntireRow.Delete
            Else
                y = y + 1
            End If
        Loop
        x = x + 1
        y = x + 1
    Loop
End Sub
<pre>

Please help to share or comment if got better ideas.
Thank You.