Macro To Find Duplicates And Put In Another Column

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,783
Office Version
  1. 365
Platform
  1. Windows
I need a code please to find duplicates in column A and find the numbers in column B and copy to column C separated by a /

As you can see there are 2 values in orange and 2 different numbers in B, the same with the values in yellow so I need the results like the second example below.

Book1
AB
1Unique IdentifierMenu Code
2M157054300001053
3M157054300001343
4M1070060006901413
5M1070060007102217
6M1070060007300619
7M1070060006901520
8M1070060007002123
9M1070060007002224
10M1570543000012225
11M1070060006901526
12M1070060007002328
13M1070060007101729
14M1070060007101630
15M1070060025800932
16M1070060007300733
17M1070060007300834
18M1070060007300736
19M1070060007300737
20M1070060007300739
21M1070060007100945
22M1070060007400746
23M1070060006901150
24M1070060007002451
25M1070060007000953
26M1070060007101854
27M1070060007100655
28M1680823000001756
Sheet1


After code.

Book1
ABC
1Unique IdentifierMenu CodeAfter Code
2M1570543000010533
3M1570543000013433
4M107006000690141313
5M107006000710221717
6M107006000730061919
7M107006000690152020 / 26
8M107006000700212323
9M107006000700222424
10M157054300001222525
11M107006000690152620 / 26
12M107006000700232828
13M107006000710172929
14M107006000710163030
15M107006002580093232
16M107006000730073333 / 36 / 37 / 39
17M107006000730083434
18M107006000730073633 / 36 / 37 / 39
19M107006000730073733 / 36 / 37 / 39
20M107006000730073933 / 36 / 37 / 39
21M107006000710094545
22M107006000740074646
23M107006000690115050
24M107006000700245151
25M107006000700095353
26M107006000710185454
27M107006000710065555
28M168082300000175656
Sheet1


Thanks so much.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Give this a try with a copy of your worksheet.

VBA Code:
Sub Combine_Dupes()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  With Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row)
    a = .Value
    For i = 1 To UBound(a)
      If d.exists(a(i, 1)) Then
        d(a(i, 1)) = d(a(i, 1)) & " / " & a(i, 2)
      Else
        d(a(i, 1)) = a(i, 2)
      End If
    Next i
    For i = 1 To UBound(a)
      a(i, 3) = d(a(i, 1))
    Next i
    .Value = a
  End With
End Sub

Edit: Or you could try this marginally shorter one.

VBA Code:
Sub Combine_Dupes_v2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  With Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row)
    a = .Value
    For i = 1 To UBound(a)
        d(a(i, 1)) = d(a(i, 1)) & " / " & a(i, 2)
    Next i
    For i = 1 To UBound(a)
      a(i, 3) = d(a(i, 1))
    Next i
    .Value = a
    .Columns(3).TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 9), Array(3, 1))
  End With
End Sub
 
Last edited:
Upvote 1
Solution
Give this a try with a copy of your worksheet.

VBA Code:
Sub Combine_Dupes()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  With Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row)
    a = .Value
    For i = 1 To UBound(a)
      If d.exists(a(i, 1)) Then
        d(a(i, 1)) = d(a(i, 1)) & " / " & a(i, 2)
      Else
        d(a(i, 1)) = a(i, 2)
      End If
    Next i
    For i = 1 To UBound(a)
      a(i, 3) = d(a(i, 1))
    Next i
    .Value = a
  End With
End Sub

Edit: Or you could try this marginally shorter one.

VBA Code:
Sub Combine_Dupes_v2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  With Range("A2:C" & Range("A" & Rows.Count).End(xlUp).Row)
    a = .Value
    For i = 1 To UBound(a)
        d(a(i, 1)) = d(a(i, 1)) & " / " & a(i, 2)
    Next i
    For i = 1 To UBound(a)
      a(i, 3) = d(a(i, 1))
    Next i
    .Value = a
    .Columns(3).TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 9), Array(3, 1))
  End With
End Sub
Second code works great, much obliged squire.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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