VBA Concatenate Rows

ShuStar

Board Regular
Joined
Sep 9, 2015
Messages
89
Office Version
  1. 2010
Platform
  1. Windows
To All - the final thing I need to do here is to concatenate the row into a new column. Any ideas on how i can achieve this?

Ultimately, for each row starting from D3 to nth Column, I want to insert a new column to my table after Column C and just concatenate that row (after it has changed Ys to that of the column header).

There may be a better way to do it without changing Ys to Column Header name etc but the flow in my head is:
-after changing the Ys to that of the name of column header then
-insert a new column after column C, which becomes new Column D (purpose for holding concatenated values) and then concatenate the rows from Column E to nth Column
-should look like "Banana, Apples, Lemon" and not "Banana, Apples , , Lemon" as there may be blank cells from the start column to end column - example below)


VBA Code:
Sub ChangeTable()
'the below finds "Y" in a cell and replaces it to that of the name of its respective Column Header;
'I would like to adapt this code to include the functionality that after replacing Ys to that of the name of the column header, add a column after Column C and Concatenate the Row


Dim lr As Long, lc As Long, i As Long

lr = Cells.Find("*", , , , xlByRows, xlPrevious).Row

lc = Cells.Find("*", , , , xlByColumns, xlPrevious).Column

    For i = 4 To lc

        Columns(i).Replace "Y", Cells(2, i), xlWhole

    Next i

End Sub



Example:

When executing above code, the table looks like:
IDNameCountBananaApplesNectarineLemon...to nColumn
12345John1Banana
12345Sara4BananaApplesNectarine...to nColumn
12345Alex3BananaApplesLemon...to nColumn
...to nRow##################


Would want adapt above code to Concatenated the rows: (note new Concatenated column added):
IDNameCountConcatenatedBananaApplesNectarineLemon...to nColumn
12345John1Banana, ApplesBanana
12345Sara4Banana, Apples, NectarineBananaApplesNectarine...to nColumn
12345Alex4Banana, Apples, LemonBananaApplesLemon...to nColumn
...to nRow##################



Thanks inadvance Gurus, appreciate the help.
 
Brilliant, thank you very much. This is working too! Would like to thank everyone for their support!!
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You're welcome. Glad we could help. :)

BTW, we can do both jobs on the same pass through if you want (though it may actually be slower to do it this way. :)).

VBA Code:
Sub Concat_Fruit_v2()
  Dim HdrAddr As String, LastCol As String
  Dim Rw As Range
  
  Columns(4).Insert
  LastCol = Split(Cells(2, Columns.Count).End(xlToLeft).Address, "$")(1)
  HdrAddr = "E2:" & LastCol & 2
  For Each Rw In Range("E3:" & LastCol & Range("A" & Rows.Count).End(xlUp).Row).Rows
    Rw.Cells(0).Value = Replace(Join(Filter(Evaluate(HdrAddr & "&""|""&" & Rw.Address), "|Y"), ", "), "|Y", "")
    Rw.Value = Evaluate("if(" & Rw.Address & "="""",""""," & HdrAddr & ")")
  Next Rw
  Columns(4).AutoFit
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,528
Messages
6,120,065
Members
448,942
Latest member
sharmarick

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