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
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: