+ Reply to Thread
Results 1 to 8 of 8

Adding a count to concatenate range function

Hybrid View

  1. #1
    Registered User
    Join Date
    02-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Adding a count to concatenate range function

    Hi all,

    I am looking to alter current coding in a concatenaterange function. See below:
    Function ConcatenateRange(ByVal cell_range As Range, _
                        Optional ByVal seperator As String) As String
    
    Dim cell As Range
    Dim newString As String
    Dim cellArray As Variant
    Dim i As Long, j As Long
    
    cellArray = cell_range.Value
    
    For i = 1 To UBound(cellArray, 1)
        For j = 1 To UBound(cellArray, 2)
            If Len(cellArray(i, j)) <> 0 Then
                newString = newString & (seperator & cellArray(i, j))
            End If
        Next
    Next
    
    If Len(newString) <> 0 Then
        newString = Right$(newString, (Len(newString) - Len(seperator)))
    End If
    
    ConcatenateRange = newString
    
    End Function
    Within the workbook I use CHAR(10) as a separator however I want the function to count the total cells to be concatenated in the range and attribute a separator such as 1) Cell Detail 1, 2) Cell Detail 2, etc. For example if I had the following data in each cell:
    A1: Thanks
    A2: for the
    A3: help!

    I want it to look like:

    B1: 1) Thanks
    2) for the
    3) help!

    Thanks in advance!
    - mbus
    Last edited by mbus; 02-12-2013 at 11:15 PM.

  2. #2
    Registered User
    Join Date
    02-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Adding a count to concatenate range function

    I am desperate!

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Adding a count to concatenate range function

    You need to wrap the code with the code tag.
    i.e
    [code]
    VBA code here
    [/code]

    It is a MUST rule here.

  4. #4
    Registered User
    Join Date
    02-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Adding a count to concatenate range function

    Apologies, I am very green to these forums. Thanks.

    Function ConcatenateRange(ByVal cell_range As Range, _
                        Optional ByVal seperator As String) As String
    
    Dim cell As Range
    Dim newString As String
    Dim cellArray As Variant
    Dim i As Long, j As Long
    
    cellArray = cell_range.Value
    
    For i = 1 To UBound(cellArray, 1)
        For j = 1 To UBound(cellArray, 2)
            If Len(cellArray(i, j)) <> 0 Then
                newString = newString & (seperator & cellArray(i, j))
            End If
        Next
    Next
    
    If Len(newString) <> 0 Then
        newString = Right$(newString, (Len(newString) - Len(seperator)))
    End If
    
    ConcatenateRange = newString
    
    End Function

  5. #5
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Adding a count to concatenate range function

    for example
    Function ConcatenateRange(ByVal cell_range As Range, _
                              Optional ByVal seperator As String) As String
    Dim cell As Range
    Dim newString As String
    Dim cellArray As Variant
    Dim i As Long, j As Long, k As Long
    
    cellArray = cell_range.Value
    For i = 1 To UBound(cellArray, 1)
        For j = 1 To UBound(cellArray, 2)
            If Len(cellArray(i, j)) Then k = k + 1: newString = newString & _
                                            seperator & k & ")" & cellArray(i, j)
        Next
    Next
    If Len(newString) Then newString = Right$(newString, (Len(newString) - Len(seperator)))
    ConcatenateRange = newString
    End Function

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Adding a count to concatenate range function

    Thanks for the code tag and I hope you can edit your first post as well.

    Here's an idea
    Function ConcatenateRange(ByVal cell_range As Range, _
                    Optional ByVal separator As String = " ") As String
        Dim e, n As Long
        For Each e In cell_range.Value
            If e <> "" Then
                n = n + 1
                ConcatenateRange = ConcatenateRange & separator & n & ") " & e
            End If
        Next
        If Len(ConcatenateRange) Then ConcatenateRange = Mid$(ConcatenateRange, Len(separator) + 1)
    End Function

  7. #7
    Registered User
    Join Date
    02-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Adding a count to concatenate range function

    Jindon, you are a life saver. It works perfectly thank you! +rep!

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Adding a count to concatenate range function

    You are welcome and thanks for the edit.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1