Get second column value by comma separated based on first value

Dorababu

New Member
Joined
Nov 24, 2011
Messages
22
Hi all I have an excel with the following column values
ABCXYZ
ABCMNO
ABCPQR
WERVRT
WERHYT
WERHIJ

<tbody>
</tbody>

What I need is a macro that will match all the duplicates and give me a result as follows in the next cell

ABCXYZABC-XYZ,MNO,PQR
ABCMNO
ABCPQR
WERVRTWER-VRT,HYT,HIJ
WERHYT
WERHIJ

<tbody>
</tbody>

 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG16Jan50
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Temp [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Temp [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]Set[/COLOR] Temp = Dn
        Temp.Offset(, 2) = Dn.Value & "- " & Temp.Offset(, 1)
    [COLOR="Navy"]ElseIf[/COLOR] Not Dn.Value = Dn.Offset(-1).Value [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]Set[/COLOR] Temp = Dn
        Temp.Offset(, 2) = Dn.Value & "- " & Temp.Offset(, 1)
    [COLOR="Navy"]Else[/COLOR]
        Temp.Offset(, 2) = Temp.Offset(, 2) & "," & Dn.Offset(, 1)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 

Dorababu

New Member
Joined
Nov 24, 2011
Messages
22
Try this:-
Code:
[COLOR=Navy]Sub[/COLOR] MG16Jan50
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range, Temp [COLOR=Navy]As[/COLOR] Range
[COLOR=Navy]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Rng
    [COLOR=Navy]If[/COLOR] Temp [COLOR=Navy]Is[/COLOR] Nothing [COLOR=Navy]Then[/COLOR]
        [COLOR=Navy]Set[/COLOR] Temp = Dn
        Temp.Offset(, 2) = Dn.Value & "- " & Temp.Offset(, 1)
    [COLOR=Navy]ElseIf[/COLOR] Not Dn.Value = Dn.Offset(-1).Value [COLOR=Navy]Then[/COLOR]
        [COLOR=Navy]Set[/COLOR] Temp = Dn
        Temp.Offset(, 2) = Dn.Value & "- " & Temp.Offset(, 1)
    [COLOR=Navy]Else[/COLOR]
        Temp.Offset(, 2) = Temp.Offset(, 2) & "," & Dn.Offset(, 1)
    [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]Next[/COLOR] Dn
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick

Thanks a ton Mick
 

Dorababu

New Member
Joined
Nov 24, 2011
Messages
22

ADVERTISEMENT

Hi one small help I have a different approach

ADILABAD

<tbody>
</tbody>
ADILABAD

<tbody>
</tbody>
ANKAPOORADILABAD;ADILABAD;ANKAPOOR;ANKOLI
ANKOLI
ASIFABADADDAGHATADILABAD;ASIFABAD;ADDAGHAT;ANKUSAPUR
ANKUSAPUR
ANANTAPURAGALIAGALIANANTAPUR;AGALI;AGALI;AKKAGALADEVARAHALLI
AKKAGALADEVARAHALLI
AMADAGURAMADAGURANANTAPUR;AMADAGUR;CHEEKIREVULAPALLE
CHEEKIREVULAPALLE

<tbody>
</tbody>

Can you help me on this
 
Last edited:

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
What you are asking is not clear.
Please explain your requirement as in your original Post, showing example of data, Before and After code has run !!!!
 

Dorababu

New Member
Joined
Nov 24, 2011
Messages
22
Here is my data

ADILABADADILABADANKAPOOR
ADILABADADILABADANKOLI
ADILABADASIFABADADDAGHAT
ADILABADASIFABADANKUSAPUR
ANANTAPURAGALIAGALI
ANANTAPURAGALIAKKAGALADEVARAHALLI
ANANTAPURAMADAGURAMADAGUR
ANANTAPURAMADAGURCHEEKIREVULAPALLE

<tbody>
</tbody>

Expected

ADILABADADILABADANKAPOORADILABAD;ADILABAD;ANKAPPOR;ANKOLI
ADILABADADILABADANKOLI
ADILABADASIFABADADDAGHATADILABAD;ASIFABAD;ADDAGHAT;ANKUSAPUR
ADILABADASIFABADANKUSAPUR
ANANTAPURAGALIAGALIANANTAPUR;AGALI;AGALI;AKKAGALADEVARAHALLI
ANANTAPURAGALIAKKAGALADEVARAHALLI
ANANTAPURAMADAGURAMADAGURANANTAPUR;AMADAGUR;AMADAGUR;CHEEKIREVULAPALLE
ANANTAPURAMADAGURCHEEKIREVULAPALLE

<tbody>
</tbody>
 
Last edited:

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG19Jan23
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Temp [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Temp [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]Set[/COLOR] Temp = Dn
        Temp.Offset(, 3) = Dn.Value & ";" & Dn.Offset(, 1).Value & ";" & Temp.Offset(, 2)
    [COLOR="Navy"]ElseIf[/COLOR] Not Dn.Value & Dn.Offset(, 1).Value = Dn.Offset(-1).Value & Dn.Offset(-1, 1).Value [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]Set[/COLOR] Temp = Dn
        Temp.Offset(, 3) = Dn.Value & ";" & Dn.Offset(, 1).Value & ";" & Temp.Offset(, 1)
    [COLOR="Navy"]Else[/COLOR]
        Temp.Offset(, 3) = Temp.Offset(, 3) & "," & Dn.Offset(, 2)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 

Watch MrExcel Video

Forum statistics

Threads
1,122,482
Messages
5,596,402
Members
414,063
Latest member
N_Bates

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
Top