Hi,
I am trying to concatenate a row which has some 150 entries
=A1&","&A2&","&A3","&A4...... and on like this.
But i find it teadous to do each and every cell.
Any solution to simplify?
Concatenate more than 10 words
-
- 2StarLounger
- Posts: 183
- Joined: 26 Dec 2010, 06:56
Concatenate more than 10 words
Regards
Saras
Saras
-
- Administrator
- Posts: 78549
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Concatenate more than 10 words
You could create the following custom function in a module in the Visual Basic Editor:
Use like this in a cell formula:
=CONCAT(A1:A12)
to concatenate the values separated by commas (the default), or
=CONCAT(A1:A12," - ")
to concatenate the values separated by spaces and dashes (hyphens).
Code: Select all
Function Concat(rng As Range, Optional sep As String = ",") As String
Dim rngCell As Range
Dim strResult As String
For Each rngCell In rng
If rngCell.Value <> "" Then
strResult = strResult & sep & rngCell.Value
End If
Next rngCell
If strResult <> "" Then
strResult = Mid(strResult, Len(sep) + 1)
End If
Concat = strResult
End Function
=CONCAT(A1:A12)
to concatenate the values separated by commas (the default), or
=CONCAT(A1:A12," - ")
to concatenate the values separated by spaces and dashes (hyphens).
Best wishes,
Hans
Hans
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: Concatenate more than 10 words
Very nice function Hans.
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- Microsoft MVP
- Posts: 87
- Joined: 10 Mar 2011, 05:38
- Status: Microsoft MVP
- Location: New Jersey in the US
Re: Concatenate more than 10 words
Here is an extended concatenation UDF that I have posted in the past which works differently than the one Hans posted (in case you ever need the added functionalitly)...
This function allows you to create formulas like this...
=ConCat("-",A1:A3,C1,"HELLO",D1:D2)
and the contents of the indicated cells, and the word "HELLO", will be concantenated together, in the order shown, with a dash between them. The delimiter (first argument) is required, but if you want to concatenate cells, cell ranges or text together without a delimiter, you can specify the empty string ("") as the first argument or simply omit it (but still use the comma as a placeholder in order to delineate the argument position). So, you could concatenate my above example cells and text, but with no delimiter between them, either like this...
=ConCat("",A1:A3,C1,"HELLO",D1:D2)
or like this (note the leading comma)...
=ConCat(,A1:A3,C1,"HELLO",D1:D2)
your choice.
Â
Code: Select all
Function ConCat(Delimiter As Variant, ParamArray CellRanges() As Variant) As String
Dim Cell As Range, Area As Variant
If IsMissing(Delimiter) Then Delimiter = ""
For Each Area In CellRanges
If TypeName(Area) = "Range" Then
For Each Cell In Area
If Len(Cell.Value) Then ConCat = ConCat & Delimiter & Cell.Value
Next
Else
ConCat = ConCat & Delimiter & Area
End If
Next
ConCat = Mid(ConCat, Len(Delimiter) + 1)
End Function
=ConCat("-",A1:A3,C1,"HELLO",D1:D2)
and the contents of the indicated cells, and the word "HELLO", will be concantenated together, in the order shown, with a dash between them. The delimiter (first argument) is required, but if you want to concatenate cells, cell ranges or text together without a delimiter, you can specify the empty string ("") as the first argument or simply omit it (but still use the comma as a placeholder in order to delineate the argument position). So, you could concatenate my above example cells and text, but with no delimiter between them, either like this...
=ConCat("",A1:A3,C1,"HELLO",D1:D2)
or like this (note the leading comma)...
=ConCat(,A1:A3,C1,"HELLO",D1:D2)
your choice.
Â
-
- 2StarLounger
- Posts: 183
- Joined: 26 Dec 2010, 06:56
Re: Concatenate more than 10 words
fabulous....you guys a playing with macros on tips.
Thanks once again...
Thanks once again...
Regards
Saras
Saras
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: Concatenate more than 10 words
Thanks Rick. This is a very nifty UDF. It really amazes me what Excel (and a bit of code in Excel) can do!!! It is also surprising that Microsoft does not enhance their built in functions to operate like these. It would make it even more powerful as a business tool.
TX again! Awesome function!
TX again! Awesome function!
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.