Consulting

Results 1 to 17 of 17

Thread: Merging text from selected range of cell

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    28
    Location

    Merging text from selected range of cell

    I know the concatenate function is able to merge the text from the range of selected cells but I would like to enhance this function to suit my needs.

    I have a old document that have the text entry for a single block of text broken up into various cells. I would like to pull into a single cell this block of text that is spread across a few cells. I need to ensure that a space is automatically place in between the text from 2 cells so the text can be readable. Using the concatenate function can achieve the same result. Is there a better way.

    =concatenate(A1," ",A2)

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Function ConCat(rng As Range)
    Dim cell As Range
    Dim tmp s String

    For Each cell In rng

    tmp = tmp & cell.Value & " "
    Next cell

    tmp = Left(tmp, Len(tmp) -1)
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Feb 2009
    Posts
    28
    Location
    xld,

    I tried the codes but the result is a shown as a zero. By the way I have noticed the error of the missing a in As in the code, "Dim tmp As String". Correcting this do not change the result.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you post some sample data/workbook then?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Try changing
    [VBA]tmp = Left(tmp, Len(tmp) -1)[/VBA]

    to
    [VBA]ConCat = Left(tmp, Len(tmp) -1)[/VBA]

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Ah yes, I see I missed a line

    [vba]

    Function ConCat(rng As Range)
    Dim cell As Range
    Dim tmp s String

    For Each cell In rng

    tmp = tmp & cell.Value & " "
    Next cell

    tmp = Left(tmp, Len(tmp) -1)

    ConCat = tmp
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Mar 2009
    Posts
    14
    Location
    Atan,
    Here is a possible solution that I use to get client names from a spreadsheet for a form. It could be modified to be used anywhere.

    [VBA]Public Function GetClientName(ClientRow As Long) As String
    Dim tmpname As String

    If Len(Sheets("Sheet1").Cells(ClientRow, 9)) > 0 And _
    Len(Sheets("Sheet1").Cells(ClientRow, 10)) > 0 And _
    Len(Sheets("Sheet1").Cells(ClientRow, 11)) > 0 Then

    tmpname = Sheets("Sheet1").Cells(ClientRow, 9) & " " _
    & Sheets("Sheet1").Cells(ClientRow, 10) & " " _
    & Sheets("Sheet1").Cells(ClientRow, 11)

    ElseIf Len(Sheets("Sheet1").Cells(ClientRow, 9)) > 0 And _
    Len(Sheets("Sheet1").Cells(ClientRow, 11)) > 0 Then

    tmpname = Sheets("Sheet1").Cells(ClientRow, 9) & " " _
    & Sheets("Sheet1").Cells(ClientRow, 11)
    Else
    tmpname = ""
    End If

    GetClientName = tmpname
    End Function[/VBA]

  8. #8
    VBAX Regular
    Joined
    Feb 2009
    Posts
    28
    Location
    Guys,

    The changes made the codes to work as desire. Thanks guys.

  9. #9
    VBAX Regular
    Joined
    Feb 2009
    Posts
    28
    Location
    After trying out the UDF and I noticed that it will works fine as long as the selected range is a contiguous range of cells. But if there is a break the result will shown as a #Value! error message. Is there anything can be done so that this UDF can work even if the selected range of cells is non contiguous?

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Function ConCat(ParamArray rng())
    Dim cell As Range
    Dim tmp As String
    Dim i As Long

    For i = LBound(rng) To UBound(rng)

    For Each cell In rng(i)

    tmp = tmp & cell.Value & " "
    Next cell
    Next i

    tmp = Left(tmp, Len(tmp) - 1)
    ConCat = tmp
    End Function
    [/vba]
    Last edited by Bob Phillips; 03-06-2009 at 02:35 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    VBAX Regular
    Joined
    Feb 2009
    Posts
    28
    Location
    Xld,

    You are a saviour. Thanks

  12. #12
    VBAX Regular
    Joined
    Feb 2009
    Posts
    28
    Location
    Xld,

    Alas! An error was encountered while running the codes. The message said, "Compile error : expected array". This part of the codes was highlighted

    For i = LBound(rng) To UBound(rng)

  13. #13
    VBAX Regular
    Joined
    Feb 2009
    Posts
    28
    Location
    xld,

    It ok now. I discover the mistake I made. I did not change this part of the code. It should be "Function ConCat(ParamArray rng()).

  14. #14
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    To avoid double spaces in the output
    [VBA]
    For i = LBound(rng) To UBound(rng)
    For Each cell In rng(i)
    tmp = Trim(tmp & cell.Value) & " "
    Next cell
    Next i
    ConCat = Trim(tmp)

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    I keep the UDF ConcatIf on hand. The first three arguments mirror the arguments of SUMIF. The optional arguments Delimiter and NoDuplicates are (hopefully) self-explainitory.
    For the situation you describe, I'd use the formula
    =ConcatIf(A1:A10,"<>",A1:A10," ")
    Function ConcatIf(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
                                Optional Delimiter As String, Optional NoDuplicates As Boolean) As String
        Dim i As Long, j As Long
        With compareRange.Parent
            Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
        End With
        If compareRange Is Nothing Then Exit Function
        If stringsRange Is Nothing Then Set stringsRange = compareRange
        Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
                                                    stringsRange.Column - compareRange.Column)
        
            For i = 1 To compareRange.Rows.Count
                For j = 1 To compareRange.Columns.Count
                    If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
                        If InStr(ConcatIf, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                            ConcatIf = ConcatIf & Delimiter & CStr(stringsRange.Cells(i, j))
                        End If
                    End If
                Next j
            Next i
            ConcatIf = mid(ConcatIf, Len(Delimiter) + 1)
        End Function

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Doesn't handle a dis-contiguous range as required by the OP.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  17. #17
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    True.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •