concatenate text alternatively from 2 or more cells

mariusliviu

New Member
Joined
Jun 20, 2023
Messages
7
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
  2. Mobile
  3. Web
Hello, I hope this wasn't asked before, I've tried to search multiple ways...
Anyway, is it possible to create a formula or a VBA code that can do this (cells A and B have text, cell C should be the result):
1687298956760.png


The text in cells A and B have a line break (there are 3 rows but it can be more or less). In cell C it's done a concatenation of each line in the cell with a delimiter "|" between text in cell A and text in cell B and then a row break (char(10)) between each line in cell. The text could be only text or numbers, or symbols or a phrase...
I think it's clear enough from the image.
Is it possible to do with a formula or a macro (I prefer formula if it's easier)?

Thank you,
Marius
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Upload a Mini-Sheet instead of a picture.

You mention "two or more cells". If it is three, the result in Column C is going to be a little crowded.
 
Last edited:
Upvote 0
If the two are separated by a linefeed, this should work.
Code:
Sub AAAAA()
Dim c As Range, spl_A, spl_B
For Each c In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
spl_A = Split(c, Chr(10))
spl_B = Split(c.Offset(, 1), Chr(10))
    With c.Offset(, 2)
        .Value = spl_A(0) & " | " & spl_B(0) & Chr(10) & spl_A(1) & " | " & spl_B(1) & Chr(10) & spl_A(2) & " | " & spl_B(2)
        .WrapText = True
    End With
Erase spl_A
Erase spl_B
Next c
End Sub
 
Upvote 0
This update will allow you to use as many cells as you want.

marius
ABCD
1a b cd e fg h ia|d|g b|e|h c|f|i
Sheet9
Cell Formulas
RangeFormula
D1D1=TEXTJOIN(CHAR(10),,LET(r,REDUCE("",A1:C1,LAMBDA(s,c,s&TEXTSPLIT(c,,CHAR(10))&"|")),LEFT(r,LEN(r)-1)))
 
Upvote 0
Solution
If the two are separated by a linefeed, this should work.
Code:
Sub AAAAA()
Dim c As Range, spl_A, spl_B
For Each c In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
spl_A = Split(c, Chr(10))
spl_B = Split(c.Offset(, 1), Chr(10))
    With c.Offset(, 2)
        .Value = spl_A(0) & " | " & spl_B(0) & Chr(10) & spl_A(1) & " | " & spl_B(1) & Chr(10) & spl_A(2) & " | " & spl_B(2)
        .WrapText = True
    End With
Erase spl_A
Erase spl_B
Next c
End Sub
thank you for your solution, I think it works, I didn't try because I see that somebody else found an easier solution (for me) with a formula.
 
Upvote 0
This update will allow you to use as many cells as you want.

marius
ABCD
1a b cd e fg h ia|d|g b|e|h c|f|i
Sheet9
Cell Formulas
RangeFormula
D1D1=TEXTJOIN(CHAR(10),,LET(r,REDUCE("",A1:C1,LAMBDA(s,c,s&TEXTSPLIT(c,,CHAR(10))&"|")),LEFT(r,LEN(r)-1)))
this is awesome, I thought that it was a way to do, but I didn't know how.
in case it's a value that is missing for example:
1687330992305.png

in cell B there are only 2 rows, not 3 like in A.
it can be modified that formula? I really don't understand how those formulas work, I never tried LET, REDUCE and LAMBDA.
sorry but I can't install add-ons (xls2bb).
 
Upvote 0
Use same formula, offered from lrobbo314, only modify the range:
=TEXTJOIN(CHAR(10),,LET(r,REDUCE("",A1:B1,LAMBDA(s,c,s&TEXTSPLIT(c,,CHAR(10))&"|")),LEFT(r,LEN(r)-1)))
 
Upvote 0
Use same formula, offered from lrobbo314, only modify the range:
=TEXTJOIN(CHAR(10),,LET(r,REDUCE("",A1:B1,LAMBDA(s,c,s&TEXTSPLIT(c,,CHAR(10))&"|")),LEFT(r,LEN(r)-1)))
that's not what I was referring :)
in case that in cell B1 there are only 2 rows (instead of 3 in this example), the result will be #N/A and if there is a space, it will insert one more "|".
1687333905663.png
 
Upvote 0
Space is a character.
 

Attachments

  • 1687334291843.png
    1687334291843.png
    4.2 KB · Views: 8
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,655
Members
449,113
Latest member
Hochanz

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