VBA - Concatenate based on values in another cell

hoodedrobin1

New Member
Joined
Jun 19, 2017
Messages
10
I have some advance concatenate questions I am having a problem solving.

I have some data that is incorrectly broken into separate rows when scraping data from a website. I want to be able to combine the rows in question into the same row as text found on the left of said column.

Hopefully below someone can see how I need to do. The fifth column (E) is not in one cell and needs to be grouped based on text to the left.

concatenate column e into row with text
for exable E1&E2 concatenate into E2

<tbody>
</tbody><colgroup><col></colgroup>

also

concatenate column e into row with text
for exable E20&E21&E22 concatenate into E21

<tbody>
</tbody><colgroup><col></colgroup>


Saved
Parker, PeterPPHero12008Dog15/4/2017Yes
Saved
Kent, ClarkCKHero12008Dog15/15/2017Yes
Saved
Gardner, GuyGGHero12008Dog15/4/2017Yes
Saved
Stark, TonyTSHero12208Cat25/5/2017Yes
Saved
Rodgers, SteveSRHero12208Cat25/15/2017Yes
Saved
Wayne, BruceBWHero12208Cat25/4/2017
Saved
Allen, BarryBAHero12408Girl15/6/2017Yes
Saved
Xavier, CharlesCXHero12408Girl15/16/2017Yes
Saved
Grey, JeanJGHero12408Family15/4/2017Yes
Of 5
Saved
Banner, BruceBBHero12708Bus15/8/2017Yes
Defeated Villian
Saved
Summers, ScottSSHero12408Family15/4/2017Yes
Of 5

<tbody>
</tbody><colgroup><col><col><col><col><col><col><col><col></colgroup>
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I can't be 100% certain that this is right because I'm not clear on whether the blank lines belong to the cell above or below them but this does what you said in your description for the data posted:

Code:
Public Sub ConcatenateColumnE()

Dim lastRow As Long
Dim thisRow As Long
Dim currentValue As String

' Find the last row with an entry in column E
lastRow = Cells(Rows.Count, 5).End(xlUp).Row

' Start on the first row
thisRow = 1

' Keep going until the end
Do While thisRow <= lastRow
    ' Does this row have an empty cell in column A?
    If Trim$(Cells(thisRow, 1).Value) = "" Then
        ' Remember the value from column E and remove the row
        currentValue = currentValue & Cells(thisRow, 5).Value & vbCrLf
        Rows(thisRow).Delete xlShiftUp
        
        ' Since we've removed a row, the last row has moved up one
        lastRow = lastRow - 1
    Else
        ' Enter the remembered values from column E
        Cells(thisRow, 5).Value = currentValue & Cells(thisRow, 5).Value
        currentValue = ""
        
        ' Process the next row
        thisRow = thisRow + 1
    End If
Loop

End Sub

WBD
 
Upvote 0
I can't be 100% certain that this is right because I'm not clear on whether the blank lines belong to the cell above or below them but this does what you said in your description for the data posted:
WBD

Wideboydixon, I color coded them. Some lines have 2 rows of data in column E some have 3 or 4.

At the bottom you can see that there are 2 blanks above Banner, Bruce but only "Saved, Bus, Defeated Villain" is related, not "Of 5"

-HoodedRobin1
 
Upvote 0
Ahh. Now the colours make sense! What's the logic for deciding which row the extra cells belong to? I assume your sheet is not colour-coded.

WBD
 
Upvote 0
I actually set my test sheet up incorrectly... (I had to change every field because it was proprietary)

When I ran your code, it was legible and made sense (on my real data)



So thank you again!
 
Upvote 0

Forum statistics

Threads
1,214,626
Messages
6,120,602
Members
448,974
Latest member
ChristineC

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