VBA - One records of each based on 2 columns


I have this excel file in sheet1 where i can have multiple time per rows the  same 2 values from column Q and R. In this sheet, i can have up to 35000 rows of data.

I need to get a single combination recap of the column Q and R  in my sheet 2 column D and E starting at row 11.

In sheet 1 i can have:
The result i should see in my sheet 2 should be:
At the end, it should sort the result by column D.
How can i do this so that it does not run too long due to the big volume of rows?

Thanks for your help.
LVL 11
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Rodney EndrigaData AnalystCommented:
Try this code:

Sub Place_UniqueValues_from_2COL()
    Dim dict As Object, rCell As Range
    Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare
    With Sheets("Sheet1")
        For Each rCell In .Range("Q2:Q" & .Cells(.Rows.Count, "Q").End(xlUp).Row)
            dict.Item(rCell.Value & "|" & rCell.Offset(, 1).Value) = Empty
        Next rCell
    End With

    For Each V In dict.Keys()
        With Sheets("Sheet2")
            .Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).Value = Left(V, InStr(V, "|") - 1)
            .Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).Value = Right(V, Len(V) - InStr(V, "|"))
        End With
    Next V
End Sub

Open in new window


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
Wilder1626Author Commented:
This is exactly what i needed. Thank you for your help
Rodney EndrigaData AnalystCommented:
No problem, glad to assist!
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Saurabh Singh TeotiaCommented:
Alternatively you don't have to use loop in this case..you can use excel in built feature which they introduced in 2007 onwards which will be comparatively faster to do this is remove duplicates.. which is this.. The below mentioned code will do exactly what you are looking for and it will be faster...

Sub movedata()
    Dim lrow As Long
    Dim ws As Worksheet, ws1 As Worksheet
    Dim lr As Long

    Set ws = Sheets("Sheet1")
    Set ws1 = Sheets("Sheet2")
    lrow = ws.Cells(Cells.Rows.Count, "Q").End(xlUp).Row
    lr = ws1.Cells(Cells.Rows.Count, "d").End(xlUp).Row

    If lr > 10 Then ws1.Range("D11:E" & lr).ClearContents

    ws.Range("Q2:R" & lrow).Copy ws1.Range("D11")
    lr = ws1.Cells(Cells.Rows.Count, "d").End(xlUp).Row

    ws1.Range("D11:E" & lr).RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo

End Sub

Open in new window

Wilder1626Author Commented:
I will try this right now. will let you know shortly.
Wilder1626Author Commented:
wow, again, just did it with 65000 records and it took me about 2 seconds max and with the other options, about 10 seconds.

Huge difference.

Thank you so much.
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.