Concatenate with line break

rps199

New Member
Joined
Mar 31, 2015
Messages
10
Hello All!

I'm hoping someone can please help me with a formula that used to work back in 2015 but now I need it again and it's not quite working right and I've lost too many hours trying to work it out.

This is what I'm failing to achieve.

Here's an example to give you an idea of the structure I'm working with, values in B are on separate rows.

[TABLE="class: cms_table, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]Fruit[/TD]
[TD]Banana[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Apple[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Strawberry[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Veg[/TD]
[TD]Carrot[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Broccoli[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Peas[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Potatoes[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Leeks[/TD]
[/TR]
</tbody>[/TABLE]


[TABLE="class: cms_table, width: 519"]
<tbody>[TR]
[TD]So I need to combine the answers in B into a single cell like this
[TABLE="class: cms_table, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]Fruit[/TD]
[TD]Banana
Apple
Strawberry[/TD]
[/TR]
[TR]
[TD]Veg[/TD]
[TD]Carrot
Broccoli
Peas
Potatoes
Leeks[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]


The formula I was kindly given is below. My problem is the script runs but then stops on the first value in column A. I have over 5,000 values in column A so impossible to do one by one.

Many thanks in advance to anyone who can help!

Here is the original thread for reference.
https://www.mrexcel.com/forum/excel-questions/845809-concatenate-line-break-help-please.html
Code:
Sub Macro()
    Dim totalRows As Integer
    Dim arrIndexes() As Integer
    Dim intIndexesCount As Integer
    Dim temp As String
    intIndexesCount = 0
    
    totalRows = Sheet1.Range("B1").End(xlDown).Row
    
    ReDim arrIndexes(totalRows)
    
    ' Gets the indexes of the Cells in column A which have a value
    For j = 1 To Sheet1.Range("B1").End(xlDown).Row
        If Cells(j, "A") <> "" Then
            arrIndexes(intIndexesCount) = j
            intIndexesCount = intIndexesCount + 1
        End If
    Next
    
    ' Adds all the values of column B together in one cell
    For i = 0 To intIndexesCount - 1
        temp = ""
        
        If i + 1 > intIndexesCount - 1 Then
            upperLimit = totalRows
        Else
            upperLimit = arrIndexes(i + 1) - 1
        End If
        
        For RowIndex = arrIndexes(i) To upperLimit
            temp = temp & Cells(RowIndex, "B") & vbNewLine
        Next
        
        Cells(arrIndexes(i), "B") = Left(temp, Len(temp) - 1)
        
    Next
    
    Sheet1.Columns("B").EntireColumn.AutoFit
    
End Sub
 
Last edited by a moderator:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Re: Concatenate with line break - help please!! Code is not working!

How about
Code:
Sub Concat()
   
   Dim Rng As Range
   
   For Each Rng In Columns(2).SpecialCells(xlConstants).Areas
      Rng.Resize(1).Value = Join(Application.Transpose(Rng), vbLf)
   Next Rng
   [COLOR=#0000ff]Columns(1).SpecialCells(xlBlanks).EntireRow.Delete[/COLOR]
   
End Sub
This also deletes the extra rows, if you don't want that then remove the line in blue
 
Upvote 0
Re: Concatenate with line break - help please!! Code is not working!

Thank you very much for taking the time to look at this. The code works but not quite as I need it! It merges the entire column B into one cell rather than stopping when it finds another value in A and merging that section. Also need a carriage return after every line column B.

Is this possible with your code?
 
Upvote 0
Re: Concatenate with line break - help please!! Code is not working!

In the sample data you supplied there is a blank row, between the col B groups. If that is not what your data is like, could you please post an accurate sample of your data?
 
Upvote 0
Re: Concatenate with line break - help please!! Code is not working!

In the sample data you supplied there is a blank row, between the col B groups. If that is not what your data is like, could you please post an accurate sample of your data?

1000 apologies! Your code is perfect. The data sample I tested it on didn't have spaces! looking at the actual data I need to sort I can see that in some areas the space is missing. :eeek:

Thanks again!
 
Upvote 0
Re: Concatenate with line break - help please!! Code is not working!

Ok, if some are missing the blank row, try this
Code:
Sub Concat()
   
   Dim Rng As Range
   
   For Each Rng In Columns(1).SpecialCells(xlBlanks).Areas
      Rng.Offset(-1, 1).Resize(1).Value = Join(Application.Transpose(Rng.Offset(-1, 1).Resize(Rng.Count + 1)), vbLf)
   Next Rng
   Columns(1).SpecialCells(xlBlanks).EntireRow.Delete
   
End Sub
 
Upvote 0
Re: Concatenate with line break - help please!! Code is not working!

Ok, if some are missing the blank row, try this
Code:
Sub Concat()
   
   Dim Rng As Range
   
   For Each Rng In Columns(1).SpecialCells(xlBlanks).Areas
      Rng.Offset(-1, 1).Resize(1).Value = Join(Application.Transpose(Rng.Offset(-1, 1).Resize(Rng.Count + 1)), vbLf)
   Next Rng
   Columns(1).SpecialCells(xlBlanks).EntireRow.Delete
   
End Sub

This is perfect! You have saved me many hours work. Thanks a million!! :)
 
Upvote 0
Re: Concatenate with line break - help please!! Code is not working!

Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,984
Messages
6,175,786
Members
452,669
Latest member
reeseann

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