Concatenate value from A where B values equal

evanscm3

New Member
Joined
Nov 4, 2009
Messages
12
Need an expert to help me out with this one -

I have two columns A,B with (n) rows of data.
A contains unique vales. B contains non-unique values.


I need to concatenate all of the values from Column A together where their B values are equal using ; as delimiter. It doesnt matter where the conc string goes (in a new row underneath each group?

For example,

1,A
2,A
3,B
4,B

Becomes:

1,A
2,A
1;2

3,B
4,B
3;4



Any ideas?
Thanks
Chris
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi
Assuming you have value in A1 that you want to concatentate in C1 if the values in A1 and B1 match use the following formula:
=IF(A1=B1,A1&";"&B1)
Good luck.
Tony
 
Upvote 0
Thanks - not quite... sorry if i wasnt clear enough.

Column A contains n rows of values which i need to concatenate together across all rows which have the same value in B..

...so basically a group of rows which have the same value in B, conc all of their 'a' values together.
 
Upvote 0
Assuming the column B values are grouped together, here is one way that I think does what you want. It also assumes that there is a heading row so the actual values start in row 2. Results are placed in column C beside the last item in each column B group.

Test in a copy of your workbook.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> CocatGroups()<br>    <SPAN style="color:#00007F">Dim</SPAN> Data, Results<br>    <SPAN style="color:#00007F">Dim</SPAN> lr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    lr = Range("A" & Rows.Count).End(xlUp).Row<br>    <SPAN style="color:#00007F">ReDim</SPAN> Results(1 <SPAN style="color:#00007F">To</SPAN> lr, 1 <SPAN style="color:#00007F">To</SPAN> 1) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Range("A1:B" & lr)<br>        Data = .Value<br>        <SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> lr<br>            <SPAN style="color:#00007F">If</SPAN> Data(i, 2) = Data(i - 1, 2) <SPAN style="color:#00007F">Then</SPAN><br>                Results(i, 1) = Results(i - 1, 1) & ";" & Data(i, 1)<br>                Results(i - 1, 1) = ""<br>            <SPAN style="color:#00007F">Else</SPAN><br>                Results(i, 1) = Data(i, 1)<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> i<br>        .Offset(, 2).Resize(, 1).Value = Results<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
How would I alter the above code if the data I am wanting to compare is in column A & I need it to concatenate column B?

Thanks!!
 
Upvote 0
How would I alter the above code if the data I am wanting to compare is in column A & I need it to concatenate column B?

Thanks!!
From what I can understand of this you just want a reversal of the previous situation. In that case try this.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> CocatGroups_v2()<br>    <SPAN style="color:#00007F">Dim</SPAN> Data, Results<br>    <SPAN style="color:#00007F">Dim</SPAN> lr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    lr = Range("A" & Rows.Count).End(xlUp).Row<br>    <SPAN style="color:#00007F">ReDim</SPAN> Results(1 <SPAN style="color:#00007F">To</SPAN> lr, 1 <SPAN style="color:#00007F">To</SPAN> 1) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Range("A1:B" & lr)<br>        Data = .Value<br>        <SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> lr<br>            <SPAN style="color:#00007F">If</SPAN> Data(i, 1) = Data(i - 1, 1) <SPAN style="color:#00007F">Then</SPAN><br>                Results(i, 1) = Results(i - 1, 1) & ";" & Data(i, 2)<br>                Results(i - 1, 1) = ""<br>            <SPAN style="color:#00007F">Else</SPAN><br>                Results(i, 1) = Data(i, 2)<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> i<br>        .Offset(, 2).Resize(, 1).Value = Results<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>

Sample data/results:

Excel Workbook
ABC
1H1H2
2A1
3A21;2
4B3
5B43;4
6
Concat
 
Upvote 0
Thanks!!! That is what I needed! I tried several combinations and I guess I changed too many of 2's but now that I see your script, it makes sense.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,328
Members
452,907
Latest member
Roland Deschain

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