Remove duplicates from an array

L. Howard

Well-known Member
Joined
Oct 16, 2012
Messages
4,514
All the code preceding this line work just fine, and this line of code dumps myArr into column A2 of Sheet("A Name of Sheet") and down 12,000 rows.

Of those 12,000 entries, there are 0 to 7 duplicates on any give execution of the code.

How can I remove those duplicates, if they do occur from myArr before dumping into column A?

Code:
 myArr = .Range("A2:A" & LRow)

Thanks.
Howard
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
All the code preceding this line work just fine, and this line of code dumps myArr into column A2 of Sheet("A Name of Sheet") and down 12,000 rows.

Of those 12,000 entries, there are 0 to 7 duplicates on any give execution of the code.

How can I remove those duplicates, if they do occur from myArr before dumping into column A?

Code:
 myArr = .Range("A2:A" & LRow)
What version of Excel are you using?
 
Upvote 0
Hi Rick,

I have 2010.
Good, then you do not need to assign it to an array in order to delete duplicates, you can delete them directly on the worksheet. Assuming that dot in front of your Range object in your first message is referring to a With statement whose object references a worksheet, you should be able to use this to remove your duplicates..

.Range("A2:A" & LRow).RemoveDuplicates 1
 
Upvote 0
Hi Rick,

Here is the entire with statement, it doesn't like the 1 at the end of the RemoveDuplicates, errros out in red. Tried it without the 1 and it did dump into column A but had two duplicates and the line to post the myArr into sheet Publish Data errors out in yellow.

Howard

Code:
k = 2
With Sheets("Title Builder")
myArr = .Range("AE2:AO2001")
  For i = LBound(myArr) To UBound(myArr)
    n = 0
    For j = 1 To 11 Step 2
        If Len(Replace(myArr(i, j), " ", "")) > 0 Then
            ReDim Preserve arrOut(n)
            arrOut(n) = myArr(i, j)
            n = n + 1
        End If
    Next
    .Cells(k, 1).Resize(rowsize:=UBound(arrOut) + 1) = _
        WorksheetFunction.Transpose(arrOut)
    k = k + UBound(arrOut) + 1
  Next

    LRow = .Cells(Rows.Count, 1).End(xlUp).Row
    myArr = .Range("A2:A" & LRow).RemoveDuplicates 1
    
End With

Sheets("Publish Data").Range("B2").Resize(rowsize:=UBound(myArr)) = myArr
 
Upvote 0
Woops, I think I read you reply wrong.

Here is the next thing I did but it did not remove the dupes. (Had three this time, using the Remove Duplicates from the ribbon to check.)

Code:
    LRow = .Cells(Rows.Count, 1).End(xlUp).Row
    myArr = .Range("A2:A" & LRow)
                 .Range("A2:A" & LRow).RemoveDuplicates 1

Howard
 
Upvote 0
I think you should remove the duplicates before making it an array.



Rich (BB code):
.Range("A2:A" & LRow).RemoveDuplicates Columns:=1, Header:=xlYes
 
Upvote 0
So I have done this to try to take care of out puts to both Title Builder sheet and Publish Data sheet but the dupes persist. The last three tests all produced 2-3 dupes each code run and the code did not remove them, according to the Data > Remove Duplicates.

I have heard hints that the built in Remove Duplicates can be wrong. I have tried to test that with a countif>1 formula pulled down 12000 rows with a sum at the bottom. Both always agreed on the number of duplicates.

Howard

Code:
k = 2
With Sheets("Title Builder")
myArr = .Range("AE2:AO2001")
  For i = LBound(myArr) To UBound(myArr)
    n = 0
    For j = 1 To 11 Step 2
        If Len(Replace(myArr(i, j), " ", "")) > 0 Then
            ReDim Preserve arrOut(n)
            arrOut(n) = myArr(i, j)
            n = n + 1
        End If
    Next
    .Cells(k, 1).Resize(rowsize:=UBound(arrOut) + 1) = _
        WorksheetFunction.Transpose(arrOut)
    k = k + UBound(arrOut) + 1
  Next

    LRow = .Cells(Rows.Count, 1).End(xlUp).Row
    myArr = .Range("A2:A" & LRow)
            .Range("A2:A" & LRow).RemoveDuplicates 1
End With

With Sheets("Publish Data")
  .Range("B2").Resize(rowsize:=UBound(myArr)) = myArr
  .Range("B2:B" & LRow).RemoveDuplicates 1
End With
 
Upvote 0
Hi aminex,


The array is from every other column AE to AO to be posted in column A.

myArr = .Range("AE2:AO2001")

Howard
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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