Just to clarify : I don't want to remove duplicates rows, I want to remove Duplicate Cells within a row
So here's a classic address table, and in some row there's duplicate entries I need to remove those entries. Most of what I've seen in VBA is used to remove duplicates values within a column, but I can't find a way to remove duplicate values within a row.
Name | Address1 | Address2 | City | Country
Peter | 2 foobar street |2 foobar street | Boston | USA
And I want it to be like :
Name | Address1 | Address2 | City | Country
Peter | 2 foobar street | | Boston | USA
I've write a macro that will loop through all the rows and then every columns for each rows, but I have no clue as to how to spot duplicate within teh different cells within teh same row.
here's the code below:
Sub Removedupe()
Dim LastRow As Long
Dim LastColumn As Long
Dim NextCol As Long
LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastColumn = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
For counterRow = 1 To LastRow
'I'm stuck here: how to remove a duplicate values within that row?
Next counterRow
End Sub
In your case, the duplicates are adjacent. To clear duplicates in either a single column or single row for this special case:
This code is an example for row #13
Maybe this in your loop:
Probably the easiest would be with a dictionary. Read the current cell. If it is already in the dictionary then blank out the cell, otherwise add it to the dictionary.
More on dictionary here: Does VBA have Dictionary Structure?
PS: Note that my solution removes all duplicates, not just if they are in the 2nd and 3rd column as in your example.
Maybe this will solve your problem: