How do I Concatenate Multiple Rows or columns. (In my case 100)

jason6084

New Member
Joined
Aug 13, 2009
Messages
7
I have several rows of data that contains data in the following format.
123,
abc,
456,
def,
789,

What I would like to do is Concatenate them all together to get the result
123,abc,456,def,789,

I have a total of 100 rows like this that I am trying to concatenate. I have tried all sorts of things but it seems most things I have found are only good for concatenating a couple of items.

If needed I can paste transpose the rows into columns if it is easier to Concatenate multiple columns.

Either way my goal is string the items together.

Any help is greatly appreciated!
 
"Falling down" how? Can you orient me as to the problem you (think you) are having so I know where to look?

. I was trying to pull the 2 Replace parts apart to understand them.

. So I wanted to bring a final version to a form without any Replaces in, but was such that it was easy then to follow the logic of putting the Replaces in.

. I successfully removed 1 replace. As you see from my returned code version of your Sub arisConcatenating2() which “works.”
But I could not take it that last stage further to remove the remaining Replace.

. Hope that makes sense. Sorry to be pedantic. I am keen to learn from what is an amazing “One Liner” that you have produced.

. So to summarize I would greatly appreciate a final version without the Replaces in, but is of a form that is clear to see how The two replaces fit in nicely. I expect it may not be too simple. Possibly VBA puts in my “” & or & “” as it sees fit making it almost impossible to second guess as it were

. I am possibly way out of my Depth here. Clearly you have an in knowledge and experience to generate those code “from instinct” as it were?

. Alan


. Hope that makes sense.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
. I was trying to pull the 2 Replace parts apart to understand them.

. So I wanted to bring a final version to a form without any Replaces in, but was such that it was easy then to follow the logic of putting the Replaces in.

Here is what that line would look like with the Replace function calls removed...

Code:
rngD.Value = Evaluate("'" & wks.Name & "'!A1:A" & lr & "&"" ; ""&'" & wks.Name & "'!B1:B" & lr & "&"" ; ""&'" & wks.Name & "'!C1:C" & lr)
 
Upvote 0
Here is what that line would look like with the Replace function calls removed...
.......

. Ok, thanks Rick for the Patience.

. Clearly getting those “ & just right requires a level and depth understanding that I way beyond me.

All these code work, but I cannot quite get the substitutions clear in my head.

Code:
[color=blue]Sub[/color] arisConcatenating2a()
  [color=blue]Dim[/color] wks [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wks = ThisWorkbook.Worksheets("Aris") [color=lightgreen]'Sheet Info[/color]
  [color=blue]Dim[/color] lr [color=blue]As[/color] Long: [color=blue]Let[/color] lr = wks.Cells(Rows.Count, 1).End(xlUp).Row 'Last Row in Sheet
  [color=blue]Dim[/color] rngD [color=blue]As[/color] Range: [color=blue]Set[/color] rngD = wks.Range("D1:D" & lr & "") [color=lightgreen]'Output Range[/color]
  rngD.Clear 'Just so I know the next lines work!!
  
  [color=lightgreen]'Concatente with Evaluate[/color]
  rngD.Value = Evaluate(Replace(Replace("@A1:A#&"" ; ""&@B1:B#&"" ; ""&@C1:C#", "@", "'" & wks.Name & "'!"), "#", lr))
  rngD.Value = Evaluate(Replace(Replace("@A1:A#&"" ; ""&@B1:B#&"" ; ""&@C1:C#", "@", "'" & wks.Name & "'!"), "#", "" & lr & ""))
  
  rngD.Value = Evaluate(Replace("'" & wks.Name & "'!A1:A#&"" ; ""&'" & wks.Name & "'!B1:B#&"" ; ""&'" & wks.Name & "'!C1:C#", "#", lr))
  
  rngD.Value = Evaluate("'" & wks.Name & "'!A1:A" & lr & "&"" ; ""&'" & wks.Name & "'!B1:B" & lr & "&"" ; ""&'" & wks.Name & "'!C1:C" & lr)
  rngD.Value = Evaluate("'" & wks.Name & "'!A1:A" & lr & "&"" ; ""&'" & wks.Name & "'!B1:B" & lr & "&"" ; ""&'" & wks.Name & "'!C1:C" & lr & "")
 
[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]'arisConcatenating[/color]


Thanks so much anyway for the amazing codes
Sorry to trouble you

Alan
 
Upvote 0
. Hi
. Sorry for a late follow-up (Struggling with very old Notebook after my main computer has “died”)
.
. I think I almost have the understanding clear now. I will not labor the point again here, I maybe should have read my previous “colorful explanations”, just a couple of which only I could search out:
http://www.mrexcel.com/forum/excel-questions/696820-quotes-visual-basic-applications.html
post #9 here:
http://www.mrexcel.com/forum/excel-...le-references-indicate-range-cells-merge.html
. Not repeating those ideas again here, but in short one important idea was that of coming in an out spreadsheet and VBA worlds, a possibility with Evaluate, wildly used but badly documented and explained. Some conclusions I drew there and possibly forgot myself, was that to aid in getting syntax correct and ease of building up complicated “Strings” for the Evaluate one should at least initially stay with the explicit ides of taking the fundamental form of evaluate as
. Evaluate(“______”) where _______ is what one would in a spreadsheet write after the =
. Evaluate(“______”) is then effectively a = equivalent.
. But, the less clearly in literature explained possibility is to go into VBA world with
. “ & ------- & “ where ------- is VBA World giving syntax
. Evaluate(“___& ------- & “”___”)
And importantly
. Evaluate(“___& ------- & “”)
Which can be simplified to
. Evaluate(“___& -------)
. as is most often done. This simplification is valid only at the ( or ) end..

. Working again independently based on the above I came up with this that I think I can understand.

Code:
[color=blue]Sub[/color] arisConcatenating2b()
  [color=blue]Dim[/color] wks [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wks = ThisWorkbook.Worksheets("Aris") [color=lightgreen]'Sheet Info[/color]
  [color=blue]Dim[/color] lr [color=blue]As[/color] Long: [color=blue]Let[/color] lr = wks.Cells(Rows.Count, 1).End(xlUp).Row 'Last Row in Sheet
  [color=blue]Dim[/color] rngD [color=blue]As[/color] Range: [color=blue]Set[/color] rngD = wks.Range("D1:D" & lr & "") [color=lightgreen]'Output Range[/color]
  rngD.Clear 'Just so I know the conctnating lines work!!
  
  [color=lightgreen]'Concatenate with Evaluate[/color]
  
  rngD.Value = Evaluate("'" & wks.Name & "'!A1:A" & lr & "" & "&"" ; ""&" & "'" & wks.Name & "'!B1:B" & lr & "" & "&"" ; ""&" & "'" & wks.Name & "'!C1:C" & lr & "")
  rngD.Value = Evaluate("" & Replace("'" & wks.Name & "'!A1:A#" & "&"" ; ""&" & "'" & wks.Name & "'!B1:B#" & "&"" ; ""&" & "'" & wks.Name & "'!C1:C#", "#", "" & lr & "") & "")
  rngD.Value = Evaluate("" & Replace("" & Replace("@A1:A#" & "&"" ; ""&" & "@B1:B#" & "&"" ; ""&" & "@C1:C#", "@", "'" & wks.Name & "'!"), "#", "" & lr & "") & "")
  
  [color=lightgreen]'Final Simplification[/color]
  rngD.Value = Evaluate(Replace(Replace("@A1:A#&"" ; ""&@B1:B#&"" ; ""&@C1:C#", "@", "'" & wks.Name & "'!"), "#", lr))

[color=blue]End[/color] [color=blue]Sub[/color] [color=lightgreen]'arisConcatenating2b[/color]



. I confess on my small screen I have really strained the eyes on this, and I accept will further be extremely difficult in the Thread Code Window to follow. Hope I have not confused by my last “lurking – in” here.
. For developing the Code line by substitution, I find that extra & “” very important. The very experienced eye of course does this as they go along.. Respect!!

Alan


P.s.

......... this eliminates a horrendous number of concatenations and makes constructing the String argument for the Evaluate function much easier.

Agreed!!!!

.........

Note that I use stand in characters for the worksheet name and last row and then replace them with their actual values using two Replace function calls... .



(and would emphasise what you said that VBA actully takes lr and does not put lr in but puts in the actual "Evaluated" value, . Another thing that was confusing my initial attempt at simple substitution to develop the string.)

Words of wisdom again
 
Upvote 0
. (This post duplicated in similar Thread here:
http://www.mrexcel.com/forum/excel-...-separating-texts-semicolon.html?#post4105356
.... )

……………………….

. Hi,
. … this is just a very quick follow up, which came out of another Thread I was answering:
Excel Marco How to put multiple rows text values into single cell using macro
so I thought in parsing I would drop it in here:

.. Maybe this code version could be useful if a hundred or so columns were being used. The virtue here is that the String argument that would be used in the “Range Evaluate one – liner “ is made in a loop so avoids you having to type manually the long evaluate string argument necessary for many columns..


Code:
[color=darkgreen]'[/color]
[color=blue]Sub[/color] ArrisArmyTrayConcatenating3()
[color=blue]Dim[/color] wks [color=blue]As[/color] Worksheet: [color=blue]Set[/color] wks = ThisWorkbook.Worksheets("Aris") [color=darkgreen]'Sheet Info[/color]
[color=blue]Dim[/color] lr [color=blue]As[/color] Long: [color=blue]Let[/color] lr = wks.Cells(Rows.Count, 1).End(xlUp).Row 'Last Row in Sheet
[color=blue]Dim[/color] r [color=blue]As[/color] [color=blue]Long[/color] [color=darkgreen]'Rows in Sheet[/color]
[color=blue]Dim[/color] rngD [color=blue]As[/color] Range: [color=blue]Set[/color] rngD = wks.Range("D1:D" & lr & "") 'Output Range
  rngD.Clear [color=darkgreen]'Just so I know the conctnating lines work!![/color]
[color=blue]Dim[/color] Evalstr [color=blue]As[/color] [color=blue]String[/color] [color=darkgreen]'String argument for Evaluate "One Liner[/color]
 
[color=darkgreen]' Build string for Evalute "one liner"[/color]
 [color=blue]Dim[/color] c [color=blue]As[/color] [color=blue]Long[/color], lc [color=blue]As[/color] Long: [color=blue]Let[/color] lc = 3 [color=darkgreen]'Column, and last column[/color]
    [color=blue]For[/color] c = 1 [color=blue]To[/color] lc - 1 [color=blue]Step[/color] 1
    [color=blue]Let[/color] Evalstr = Evalstr & "" & wks.Range(wks.Cells(1, c), wks.Cells(lr, c)).Address & "" & "&"" ; ""&" [color=darkgreen]'Concatenate cell values with  ; inbetween[/color]
    [color=blue]Next[/color] c
 [color=blue]Let[/color] Evalstr = Evalstr & "" & wks.Range(wks.Cells(1, lc), wks.Cells(lr, lc)).Address & "" [color=darkgreen]'Concatenate last row without any  ;[/color]
 
[color=darkgreen]'Concatenate with Evaluate[/color]
 [color=blue]Let[/color] rngD.Value = Evaluate(Evalstr)
[color=blue]End[/color] [color=blue]Sub[/color] [color=darkgreen]'ArrisArmyTrayConcatenating3[/color]


Alan

P.s. The limit of about 100 columns is due to the evaluate argument character limit of 255, so depending on how long your strings in the cells are, your column number will be reduced (In the other Thread I was limited to 5 ! -- So the code was not much use there!! But those strings were very long!!! – For sensible length strings in the cells the code may be useful. Hence I have included it here!
 
Upvote 0
Obviously I am late to this thread (unless amitray can use it), but here is a UDF solution that doesn't rely on a loop (although it is limited to a single row range or a single column range, but not a range having more than one row *and* column). The function takes two arguments... the first, the range you want to join, is required... the second, text you wish to use as a delimiter, is optional (if omitted, there will be no delimiter between the cell text)...

Code:
Function JoinRange(varRange As Range, Optional varDelimiter As String) As String
  With WorksheetFunction
    If varRange.Columns.Count = 1 Then
      JoinRange = .Trim(Join(.Transpose(varRange.Value), varDelimiter))    [COLOR=#008000]'Join down[/COLOR]
    Else
      JoinRange = .Trim(Join(.Index(varRange.Value, 1, 0), varDelimiter))  [COLOR=#008000]'Join across[/COLOR]
    End If
    JoinRange = Replace(Replace(.Trim(Replace(Replace(JoinRange, " ", Chr(1)), _
                varDelimiter, " ")), " ", varDelimiter), Chr(1), " ")
  End With
End Function

...

=JoinRange(A1:P1,", ")

...

Useful function. Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,614
Messages
6,125,848
Members
449,266
Latest member
davinroach

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