Function that iterates cells in a column and concatenates the cells until it reads a specific string

edsokolo

New Member
Joined
Jun 2, 2014
Messages
3
The data I am working with contains blocks of cells in column C, with between 1-10 cells in them, that all have text in them. The blocks represent customer reviews. The last cell will always end with "(hide full review)".

I want to write a function that will move through the column and concatenate the cells together and then stop when it finds a cell that contains (hide full review) at end of it.

I found this function on mrexcel which almost works perfectly. The only problem it encounters is when reviews are only 1 line, it also concatenates the line after the blank cell.

Rich (BB code):
Function colconc(CellRef As Range, Delimiter As String)

Dim LoopVar As Long
Dim StartRow As Long
Dim EndRow As Long
Dim Concat As String
Dim Col As Long

Col = CellRef.Column
StartRow = CellRef.row
EndRow = CellRef.End(xlDown).row

Concat = ""

For LoopVar = StartRow To EndRow
Concat = Concat & Cells(LoopVar, Col).Value
If LoopVar <> EndRow Then Concat = Concat & Delimiter
Next LoopVar

colconc = Concat
End Function

I tried modifying the function with the bold VBA below, but I am still struggling to get it to run correctly.

Rich (BB code):
Function ColConcer(CellRef As Range, Delimiter As String)

Dim LoopVar As Long
Dim StartRow As Long
Dim EndRow As Long
Dim Concat As String
Dim Col As Long
Dim theendrow As Long
Dim txt As String
Dim c As Long

Col = CellRef.Column
Debug.Print Col
StartRow = CellRef.row
EndRow = CellRef.End(xlDown).row

For c = StartRow To EndRow
txt = Cells(c, 3).Value
If IsNumeric(InStr(1, txt, "(hide full review)")) = True Then
theendrow = Cells(LoopVar, Col)
End If
Next c

Concat = ""

For LoopVar = StartRow To theendrow
Concat = Concat & Cells(LoopVar, Col).Value
If LoopVar <> theendrow Then Concat = Concat & Delimiter
Next LoopVar

ColConcer = Concat
End Function



Any Help would be appreciated!

Best,
Eric
 
Last edited by a moderator:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Here's a start.
You should show an example of your data because I'm confused. Does your data always have a blank space after each review? Please be more specific.

Excel Workbook
AB
1This isThis is a test. It is only a test. A short test. (hide full review)
2a test.
3It is only
4a test.
5A short test. (hide full review)
6
7A short test. (hide full review)A short test. (hide full review)
8
9A short test. (hide full review)A short test. (hide full review)
10
11This isThis is a test. It is only a test. A short test. (hide full review)
12a test.
13It is only
14a test.
15A short test. (hide full review)
Sheet29



Code:
Function colconc(CellRef As Range, Delimiter As String)

Dim LoopVar As Long
Dim StartRow As Long
Dim EndRow As Long
Dim Concat As String
Dim Col As Long


Col = CellRef.Column
StartRow = CellRef.Row

If Cells(StartRow + 1, Col).Value = "" Then
colconc = Cells(StartRow, Col).Value
Exit Function
End If


EndRow = CellRef.End(xlDown).Row



For LoopVar = StartRow To EndRow
Concat = Concat & Cells(LoopVar, Col).Value
If LoopVar <> EndRow Then Concat = Concat & Delimiter
Next LoopVar

colconc = Concat
End Function
 
Upvote 0
Sorry about not being clear. My data does always has a blank after each review, but only because I wrote a macro that inserts one after each review.



AB
username
username123
recommend: Yes
title and date Loved this product! 12/17/2013
review here is part of the review
here is more text from the review
a little bit more
this is the end of the review. (hid full review)
usernameothername123
recommendno
title and dateThis product sucks! 3/23/2014
review This is a much shorter review. (hide full review)



<colgroup><col style="font-weight: bold; width: 30px;"><col style="width: 189px;"><col style="width: 382px;"></colgroup><tbody>
[TD="bgcolor: #CACACA, align: center"]1[/TD]

[TD="bgcolor: #CACACA, align: center"]2[/TD]

[TD="bgcolor: #CACACA, align: center"]3[/TD]

[TD="bgcolor: #CACACA, align: center"]4[/TD]

[TD="bgcolor: #CACACA, align: center"]5[/TD]

[TD="bgcolor: #CACACA, align: center"]6[/TD]

[TD="bgcolor: #CACACA, align: center"]7[/TD]

[TD="bgcolor: #CACACA, align: center"]8[/TD]

[TD="bgcolor: #CACACA, align: center"]9[/TD]

[TD="bgcolor: #CACACA, align: center"]10[/TD]

[TD="bgcolor: #CACACA, align: center"]11[/TD]

[TD="bgcolor: #CACACA, align: center"]12[/TD]

[TD="bgcolor: #CACACA, align: center"]13[/TD]

[TD="bgcolor: #CACACA, align: center"]14[/TD]

[TD="bgcolor: #CACACA, align: center"]15[/TD]

</tbody>
 
Upvote 0
Thank you skywriter. That worked great.

If you wouldn't mind helping out again. i am now trying to make a a macro that clears the contents of the cells after it concatenates them, i.e. A1:A5 to B1, I want to clear the contents of A1:A5.
 
Upvote 0
Make sure the sheet you want to clear is the active sheet. If this isn't the exact range you want to clear just edit the range in the code.

Code:
Sub ClearContents()

Range("A1:A5, B1").ClearContents

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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