Macro to clear duplicates based on SR Number/ID

Looking to include in these macros a way to delete the duplicate rows based on the SR Number/ID column. I want to be able to remove the duplicates that does not have the latest date imported. Below is the current macro in place which moves rows from the 'Current' tab to either the 'Closed' or 'Dial-Homes' tab based on the Status and Subject column. After they are moved, I would like for this macro to remove duplicates from the 'Closed' and 'Dial-Homes' tab based on the latest import which is column L. If it can keep the SR with the most recent import date, that will be great.

ub changestatus()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Dim lr As Long
    Dim cell As Range, rng As Range
    Dim ws As Worksheet, lr1 As Long
    Dim r As Range, ws1 As Worksheet
    
    With Sheets("Current").UsedRange.Offset(1).Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
        End With
        
    Set ws = Sheets("Current")
    Set ws1 = Sheets("Closed")
    lr = ws.Cells(Cells.Rows.Count, "A").End(xlUp).Row

    If lr > 1 Then

        Set rng = ws.Range("E2:E" & lr)

        For Each cell In rng
            lr1 = ws1.Cells(Cells.Rows.Count, "A").End(xlUp).Row + 1


            If InStr(1, cell.Value, "Solved", vbTextCompare) Or InStr(1, cell.Value, "closed", vbTextCompare) > 0 Then
                cell.EntireRow.Copy ws1.Range("A" & lr1)

                If r Is Nothing Then

                    Set r = cell
                Else
                    Set r = Union(cell, r)
                End If
            End If

        Next cell
        If Not r Is Nothing Then r.EntireRow.Delete

    End If

    Set ws = Sheets("Import")
    lr = ws.Cells(Cells.Rows.Count, "A").End(xlUp).Row

    If lr > 1 Then

        Set rng = ws.Range("E2:E" & lr)

        For Each cell In rng

            If InStr(1, cell.Value, "open", vbTextCompare) > 0 Then cell.Value = "Open"
        Next cell


    End If
    
       For Each sht In ThisWorkbook.Worksheets
        Select Case sht.Name
        Case "Import"
            With sht
                .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp)).Resize(, 100).Delete
            End With
        End Select
Next
 
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
ee_MoveData2Sheet

End Sub

Sub ee_MoveData2Sheet()

Dim wsCurrent As Worksheet, wsDialHomes As Worksheet, l As Long
Application.ScreenUpdating = False
Set wsCurrent = Sheets("Current")
Set wsDialHomes = Sheets("Dial-Homes")

With wsCurrent
l = .Cells(Rows.Count, "B").End(xlUp).Row
For x = 1 To l
    If Left(.Cells(x, 2).Value, 10) = "Prod: DCA1" Then
        .Cells(x, 2).EntireRow.Cut wsDialHomes.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        .Cells(x, 2).EntireRow.Delete Shift:=xlUp
        x = x - 1
    End If
Next x
End With
Application.ScreenUpdating = True

End Sub

Open in new window

Report-File.xlsx
AckeemKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gowflowCommented:
You say


After they are moved, I would like for this macro to remove duplicates from the 'Closed' and 'Dial-Homes' tab based on the latest import which is column L. If it can keep the SR with the most recent import date, that will be great.

What do you mean by 'duplicates' ?
1. All columns should have the same info then it is considered duplicates ?
2. if point 1 is YES then why you say: based on the latest import which is column L ???

Kindly clarify giving an example on the sheets posted of what is the final result like what you want as a final result.

Rgds/gowflow
0
aikimarkCommented:
@gowflow

Assume that SR/ID is the key.  There might be multiple rows with that key, so 'duplicate' only applies to the key values.  Given a set of rows with identical key values, AckeemK wants the row with the latest (most recent) date.
0
gowflowCommented:
Tks aikimark thought so but wanted to hear it from the horse's mouth, but seems your his appointed lawyer or helper ??? :) :)

gowflow
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

AckeemKAuthor Commented:
Thank you @aikimark for providing the clarification. Right on the money.
0
gowflowCommented:
ok still some clarification:

key is Col A you want the most recent date based on Col L but the example you provided all items in Col L have the same date and time. what do we do here ?

gowflow
0
aikimarkCommented:
Good catch, gowflow.  I would guess the workbook is not a representative sample of the actual data.

If you do face this data configuration, you could do one of the following:
1. filter by unique values (advanced filter) and then look for multiple rows by key, keeping the last one.

2. sort by key and iterate the groups, filtering by code
0
Martin LissOlder than dirtCommented:
Try this. Make a copy of your workbook or sheets first!

Sub DeleteDupes()

    Dim ws As Worksheet
    Dim lngLastRow As Long
    Dim lngRow As Long
    Dim strSR As String
    Dim strImportDate As String
    
    Application.ScreenUpdating = False
    
    For Each ws In Worksheets
        If ws.Name = "Closed" Or ws.Name = "Dial-Homes" Then
            ws.Activate
            ' Sort on SR (ascending) and import date (descending)
            ws.Sort.SortFields.Clear
            ws.Sort.SortFields.Add Key:=Range("A2:A6445"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            ws.Sort.SortFields.Add Key:=Range("L2:L6445"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            With ws.Sort
                .SetRange Range("A1:Q6445")
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            With ws
                ' The SR numbers are sorted with the newest at the top so
                ' loop through the rows and delete all but the first one for each SR
                lngLastRow = .Range("A1048576").End(xlUp).Row
                strSR = .Cells(lngLastRow, "A")
                strImportDate = .Cells(lngLastRow, "L")
                For lngRow = lngLastRow To 2 Step -1
                    If .Cells(lngRow, 1) = .Cells(lngRow - 1, 1) Then
                        .Cells(lngRow, 1).EntireRow.Delete
                    Else
                        strSR = .Cells(lngRow - 1, "A")
                        strImportDate = .Cells(lngRow - 1, "L")
                    End If
                Next
            End With
        End If
    Next
    Application.ScreenUpdating = False

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
0
AckeemKAuthor Commented:
Hi Martin, thank you for the coding! I put this macro to be ran last . However, when I run it, it takes me to the 'Dial-Homes' tab. Anyway for it to not take me to that tab but remain on the 'Table of Contents' tab while it runs? I've attached the report where I placed this macro in. It is within the "Update Links" button on the 'Table of Contents' tab.
DSE-Carelog-Report-V2.xlsm
0
Martin LissOlder than dirtCommented:
If you left line 9 in you won't see the Dial-Homes sheet until the macro ends. If you start at the Table of Contents sheet and add

Sheets("Table of Contents").Activate

Open in new window


between line 42 and 43 you won't see anything happen at all. (You could add a "Done" Msgbox). BTW line 43 should be

Application.ScreenUpdating = True
0
Martin LissOlder than dirtCommented:
BTW if you want to always go back to the sheet you started at no matter which sheet that is then use this version.

Sub DeleteDupes()

    Dim ws As Worksheet
    Dim wsStart As Worksheet
    Dim lngLastRow As Long
    Dim lngRow As Long
    Dim strSR As String
    Dim strImportDate As String
    
    Application.ScreenUpdating = False
    Set wsStart = ActiveSheet
    
    For Each ws In Worksheets
        If ws.Name = "Closed" Or ws.Name = "Dial-Homes" Then
            ws.Activate
            ' Sort on SR (ascendinh) and import date (descending)
            ws.Sort.SortFields.Clear
            ws.Sort.SortFields.Add Key:=Range("A2:A6445"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            ws.Sort.SortFields.Add Key:=Range("L2:L6445"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            With ws.Sort
                .SetRange Range("A1:Q6445")
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            With ws
                ' The SR numbers are sorted with the newest at the top so
                ' loop through the rows and delete all but the first one for each SR
                lngLastRow = .Range("A1048576").End(xlUp).Row
                strSR = .Cells(lngLastRow, "A")
                strImportDate = .Cells(lngLastRow, "L")
                For lngRow = lngLastRow To 2 Step -1
                    If .Cells(lngRow, 1) = .Cells(lngRow - 1, 1) Then
                        .Cells(lngRow, 1).EntireRow.Delete
                    Else
                        strSR = .Cells(lngRow - 1, "A")
                        strImportDate = .Cells(lngRow - 1, "L")
                    End If
                Next
            End With
        End If
    Next
    wsStart.Activate
    Application.ScreenUpdating = True

End Sub

Open in new window

0
AckeemKAuthor Commented:
Thank you for this information! Greatly appreciated.
0
Martin LissOlder than dirtCommented:
We aim to please.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.