• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 39
  • Last Modified:

VBA - One records of each based on 2 columns

Hi.

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.

Ex:
In sheet 1 i can have:
Sheet1.jpg
The result i should see in my sheet 2 should be:
Sheet-2.jpg
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.
1-record-of-each.xlsm
0
Wilder1626
Asked:
Wilder1626
  • 3
  • 2
1 Solution
 
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

0
 
Wilder1626Author Commented:
This is exactly what i needed. Thank you for your help
0
 
Rodney EndrigaData AnalystCommented:
No problem, glad to assist!
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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

0
 
Wilder1626Author Commented:
I will try this right now. will let you know shortly.
0
 
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.
0
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.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now