Concatenate & Remove Duplicate

peelamedu_bulls

New Member
Joined
Nov 30, 2005
Messages
29
I have the following column values

Input
Account no Code
000005678 ABC
000005678 EDF
000001234 ABC
000001234 ABC

Desired Output
Account no Code
000005678 ABC
000005678 EDF
000001234 ABC

Criteria
Concatenate Column A and B (i.e Account No and Code)
If there are duplicates of this concatenated data, remove duplicates

I'm able to do this in VBA in a round about way, i.e create a column to concatenate two columns and then create one more column and use countif to determine if there are duplicates and then remove the duplicate. But it's long, and not elegant. Trying to avoid these calculation columns in the final output.

Is there a more elegant solution to this problem ?
 

Some videos you may like

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,915
Office Version
365
Platform
Windows
How about
Code:
Sub peelamedu2()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("if({1}, " & .Address & " & " & .Offset(, 1).Address & ")")
      .Resize(, 2).RemoveDuplicates 1, xlNo
   End With
  [COLOR=#ff0000] Columns(2).Delete[/COLOR]
End Sub
Remove the line in red if you don't want to delete col B
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
7,845
Office Version
2016
Platform
Windows
How about
Code:
Sub peelamedu2()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("if({1}, " & .Address & " & " & .Offset(, 1).Address & ")")
      .Resize(, 2).RemoveDuplicates 1, xlNo
   End With
  [COLOR=#ff0000] Columns(2).Delete[/COLOR]
End Sub
Remove the line in red if you don't want to delete col B
Hi Fluff,

Do you know which version of excel first introduced the RemoveDuplicates functionality\Method ?

Regards.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,915
Office Version
365
Platform
Windows
I think it was either 2007, or 2010.
 

peelamedu_bulls

New Member
Joined
Nov 30, 2005
Messages
29
How about
Code:
Sub peelamedu2()
   With Range("A2", Range("A" & Rows.Count).End(xlUp))
      .Value = Evaluate("if({1}, " & .Address & " & " & .Offset(, 1).Address & ")")
      .Resize(, 2).RemoveDuplicates 1, xlNo
   End With
  [COLOR=#ff0000] Columns(2).Delete[/COLOR]
End Sub
Remove the line in red if you don't want to delete col B

Thank you.
However the output now is combined in one column, i.e Column A. I needed column A and B to be retained. Just the duplicates removed.

Desired output
Account No Code
000005678 ABC
000005678 EDF
000001234 ABC
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,915
Office Version
365
Platform
Windows
In that case how about
Code:
Sub peelamedu2()
   With Range("C2:C" & Range("A" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate("if({1}, " & .Offset(, -2).Address & " & " & .Offset(, -1).Address & ")")
      .Offset(, -2).Resize(, 3).RemoveDuplicates 3, xlNo
   End With
   Columns(3).Delete
End Sub
This will put the concatenated values in col C & then delete it
 

mohadin

Active Member
Joined
Mar 22, 2015
Messages
361
Office Version
2013
Platform
Windows
Is this how you whant
Code:
Sub do_it()
   With Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 2)
     .RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
   End With
  End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
40,915
Office Version
365
Platform
Windows
@mohadin
Using RemoveDuplicates on multiple columns like that is unreliable & I would recommend against using.
 

mohadin

Active Member
Joined
Mar 22, 2015
Messages
361
Office Version
2013
Platform
Windows
Hi fluff
Thank you, would you please explicate
 

Watch MrExcel Video

Forum statistics

Threads
1,099,505
Messages
5,469,024
Members
406,628
Latest member
jared92

This Week's Hot Topics

Top