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>

 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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
 
Upvote 0
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
 
Upvote 0
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:
Upvote 0
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 !!!!
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,344
Members
448,570
Latest member
rik81h

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