Thursday, 15 February 2024

Remove duplicate values from multiple excel files

 Sub RemoveDuplicates(sourceFolder As String, targetFolder As String)

    Dim wb1 As Workbook

    Dim wb2 As Workbook

    Dim ws1 As Worksheet

    Dim ws2 As Worksheet

    Dim lastRow1 As Long

    Dim lastRow2 As Long

    Dim i As Long

    Dim j As Long

    Dim fileName As String


    ' Get the list of files in the source folder

    fileName = Dir(sourceFolder & "\*.xlsx")

    tfileName = Dir(targetFolder & "\*.xlsx")


    ' Loop through each file in the source folder

    Do While fileName <> ""

        ' Open the source workbook

        Set wb1 = Workbooks.Open(sourceFolder & "\" & fileName)

    ' Loop through each file in the target folder

    Do While fileName <> ""

        ' Open the corresponding file in the target folder

        Set wb2 = Workbooks.Open(targetFolder & "\" & tfileName)


        ' Set worksheets

        Set ws1 = wb1.Sheets("Sheet1") ' Change "Sheet1" to the actual sheet name

        Set ws2 = wb2.Sheets("Sheet1") ' Change "Sheet1" to the actual sheet name


        ' Find the last row in each worksheet

        lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row ' Assuming the unique identifier is in column A

        lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row ' Assuming the unique identifier is in column A


        ' Loop through each row in ws1

        For i = lastRow1 To 2 Step -1

            ' Loop through each row in ws2 to find duplicates

            For j = lastRow2 To 2 Step -1

                If ws1.Cells(i, 1).Value = ws2.Cells(j, 1).Value Then ' Assuming the unique identifier is in column A

                    ' Remove the duplicate row from ws2 (target folder)

                    ws2.Rows(j).Delete

                    Exit For ' Exit the inner loop once a duplicate is found

                End If

            Next j

        Next i


        ' Close the source workbook without saving changes

        wb1.Close SaveChanges:=False


        ' Save and close the target workbook

        wb2.Close SaveChanges:=True

        

        ' Get the next file in the source folder

        tfileName = Dir()

        Loop

    

        ' Get the next file in the source folder

        fileName = Dir()

        

    Loop


    MsgBox "Duplicates removed successfully from the target folder."


End Sub



Function Duplicate()


    RemoveDuplicates "C:\Users\smeti\Desktop\Comparision\Source", "C:\Users\smeti\Desktop\Comparision\Target"


End Function