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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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
1username
username123
2recommend: Yes
3title and date Loved this product! 12/17/2013
4review here is part of the review
5here is more text from the review
6 a little bit more
7this is the end of the review. (hid full review)
8
9usernameothername123
10recommendno
11title and dateThis product sucks! 3/23/2014
12review This is a much shorter review. (hide full review)
13
14
15


<colgroup><col style="font-weight: bold; width: 30px;"><col style="width: 189px;"><col style="width: 382px;"></colgroup><tbody>
</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,215,359
Messages
6,124,488
Members
449,165
Latest member
ChipDude83

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