Remove duplicate rows macro

LT_Orange

New Member
Joined
Nov 16, 2016
Messages
1
Hello All,

I work at a test lab and i have over 500,000 test records. I need to remove the duplicate record rows. The file has this layout. A patient can have multiple test and on rare cases have a different case number.
Case NumberPatient NameTestPatient ID
1011John SmithABC1235555
1011John SmithABC1235555
1014Jane DoeYHF1625512
1014Jane DoeBRAC215512
1032Jane DoeJFEH125512
1014Jane DoeYHF1625512
1014Jane DoeBRAC215512
Case NumberPatient NameTestPatient ID
1011John SmithABC1235555
1014Jane DoeYHF1625512
1014Jane DoeBRAC325512
1032Jane DoeJFEH125512

<tbody>
</tbody>

I want to create a macro that will give me this result.

I have the following.

Code:
Sub SameLine ()

Dim x as long
Dim y as long
Dim Test as long

test = cells.find (what:="Test", LookIn =:xlformulas, lookAt:=xlPart, searchOrder=:xLByRow, SearchDirection:=XlNext, MatchCase:=false_, SearchFormat=:false).column
Dim VarStart as Long 
varstart = cells.find (what:="Case Number", LookIn =:xlformulas, lookAt:=xlPart, searchOrder=:xLByRow, SearchDirection:=XlNext, MatchCase:=false_, SearchFormat=:false).column

dim varend as long
varend = 0
do while cells (x,1).value <> ""
y=y +varend +2
rows(x+1).delete
x=x+1
loop
end sub

This code only gives me the following records.

1011John SmithABC1235555
1011John SmithABC1235555

<tbody>
</tbody>

1014Jane DoeYHF1625512

<tbody>
</tbody>


Thanks in advance.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Please make a "sheet2".
Sheet1 has data and sheet2 will have result.

Code:
Sub sample()
    Dim Dic, i As Long, LR As Long
    Dim buf As String, keys
    Set Dic = CreateObject("scripting.Dictionary")
    On Error Resume Next
    With Sheets("sheet1")
        LR = .cells(Rows.Count, 1).End(xlUp).row
        For i = 2 To LR
            buf = .cells(i, 1).Value & "_" & .cells(i, 2).Value & "_" & .cells(i, 3).Value & "_" & .cells(i, 4).Value
            Dic.Add buf, buf
        Next
    End With
    keys = Dic.keys
    With Sheets("sheet2")
        For i = 0 To Dic.Count - 1
            .Range(.cells(i + 1, 1), .cells(i + 1, 4)) = split(keys(i), "_")
        Next
    End With
    Set Dic = Nothing
End Sub
 
Upvote 0
If you are using an excel version of 2007 or later, there is a built in function on the ribbon that will do this for you. Click on the Data Tab and look for Remove Duplicates function.
 
Upvote 0
If you are using an excel version of 2007 or later, there is a built in function on the ribbon that will do this for you. Click on the Data Tab and look for Remove Duplicates function.
Sorry Alan, but it sometimes doesn't.
Unless you have a way of guaranteeing reliability of the Remove Duplicates function its use is better avoided.
For example, try it on the following data set, and if you like report your results.
4x
1010
10x4
44
1010
4x

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,634
Messages
6,120,659
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top