I have excel file in which data is present in a column as
234
654
222
444
667
555
I want this text in this form 234,654,222,444,667,555.I tried many ways bt unable to do so,Kindly tell me any easy way of doing this.
I have excel file in which data is present in a column as
234
654
222
444
667
555
I want this text in this form 234,654,222,444,667,555.I tried many ways bt unable to do so,Kindly tell me any easy way of doing this.
Formula wise it's a little convoluted unless you make use of 3rd party utilities like morefunc.xll and / or other custom UDFs of which there are plenty if you Google.
A basic example of such a UDF being:
With the above stored in a Module in VBE you can utilise from native XL by virtue of a "normal" function in which you simply specify the range containing the values to be concatenated and preferred delimiter, eg:![]()
Function CONCATSTR(rngVals As Range, strDelim As String, Optional boolCap As Boolean) Dim vData, lngCols As Long, lngCol As Long, strTemp As String lngCols = rngVals.Columns.Count If Application.CountA(rngVals) = 0 Then CONCATSTR = "Empty": Exit Function If rngVals.Rows.Count > 1 Then If lngCols = 1 Then With rngVals vData = Application.Transpose(.Parent.Evaluate("IF(" & .Address & "<>"""",SUBSTITUTE(" & .Address & ","" "",""^""),REPT(" & .Address & ",1))")) End With Else For lngCol = 1 To lngCols Step 1 If Application.CountA(rngVals.Columns(lngCol)) Then With rngVals.Columns(lngCol) vData = Application.Transpose(.Parent.Evaluate("IF(" & .Address & "<>"""",SUBSTITUTE(" & .Address & ","" "",""^""),REPT(" & .Address & ",1))")) End With If lngCol < lngCols Then CONCATSTR = CONCATSTR & Replace(Replace(Application.WorksheetFunction.Trim(Join(vData, " ")) & " ", " ", strDelim), "^", " ") End If End If Next lngCol End If Else With rngVals vData = .Parent.Evaluate("IF(" & .Address & "<>"""",SUBSTITUTE(" & .Address & ","" "",""^""),REPT(" & .Address & ",1))") End With End If CONCATSTR = CONCATSTR & Replace(Replace(Application.WorksheetFunction.Trim(Join(vData, " ")), " ", strDelim), "^", " ") If boolCap Then CONCATSTR = strDelim & CONCATSTR & strDelim End Function
would concatenate all non-blanks in the range, each value being delimited by a comma.![]()
=CONCATSTR(A1:B100,",")
Last edited by DonkeyOte; 04-28-2010 at 03:17 AM. Reason: missed the .Parent (important)
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
Hi,
Another approach would be:
Orlando Mezquita![]()
Function CONCAT(R As Range, Delim As String) As String Dim Rng As Range Dim st As String If Application.CountA(R) = 0 Then CONCAT = "Empty": Exit Function For Each Rng In R If Not (IsEmpty(Rng)) Then st = st & Delim & Rng.Value Next Rng CONCAT = Right(st, Len(st) - 1) End Function
Last edited by orlandom; 04-28-2010 at 03:14 AM. Reason: Eliminate spaces from code
Please read the Forum Rules:
If your problem is solved, mark your thread as Solved. Read Forum's rule #10 to know how to do it.
If you are happy with my answer please add it to my reputation by clicking in the icon next to the post number in my post.
"Practice isn't the thing you do once you're good. It's the thing you do that makes you good."
Malcolm Gladwell
I should elaborate perhaps in response to above variant the reasons for the apparent complexity of the first code.
The first code is designed to limit the need for iteration as much as possible and/or dispense with it fully where possible.
The code will iterate only where range is a matrix (as opposed to a vector) and then only on a column by column basis - ie cell by cell iteration is avoided.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks