+ Reply to Thread
Results 1 to 4 of 4

Want to stop my concatenate function concatenating duplicates

Hybrid View

  1. #1
    Registered User
    Join Date
    03-21-2010
    Location
    Dorset, england
    MS-Off Ver
    Excel 2007
    Posts
    16

    Want to stop my concatenate function concatenating duplicates

    I am using a custom function to concatenate a range of cells, but want to modify it so it won't concatenate any duplicated values

    any help much appreciated

    here is the function I'm using
    Function Concat(myRange As Range)
        
        Dim r As Range
        myDelimiter = "+"
        Application.Volatile
        For Each r In myRange
            If r <> "" Then
                Concatenate = Concatenate & r & myDelimiter
            End If
            
        Next r
        If Len(myDelimiter) > 0 Then
           Concat = Left(Concatenate, Len(Concatenate) - Len(myDelimiter))
        End If
    
    End Function

  2. #2
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848

    Re: Want to stop my concatenate function concatenating duplicates

    The Option Base 1 goes with the Option Explicit at the top of the module. If Option Explicit does not exist - add it.

    I suggest turning the "Require Variable Declaration" option on full time. In the VBA area, go to Tools / Options... Editor tab and check the "Require Variable Declaration" box.


    Option Base 1
    
    Function Concat(myRange As Range) As String
        Dim myDelimiter As String
        Dim r As Range
        myDelimiter = "+"
        Application.Volatile
        Dim i As Integer, x As Integer
        Dim j() As String, exists As Boolean
        ReDim j(1)
        For Each r In myRange
            If r <> "" Then
                For x = 1 To UBound(j)
                    If j(x) = r Then
                        exists = True
                        Exit For
                    Else
                        exists = False
                    End If
                Next x
                If exists = False Then
                    i = i + 1
                    ReDim Preserve j(i)
                    j(i) = r
                End If
            End If
        Next r
          Concat = Join(j, myDelimiter)
    End Function

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Want to stop my concatenate function concatenating duplicates

    Here's another way:
    Option Explicit
    
    #Const EarlyBound = False
    
    Function CatUnique(r As Range, Optional sSep As String = ",") As String
        CatUnique = Join(Unique(r), sSep)
    End Function
    
    Function Unique(r As Range, Optional bCount As Boolean = False) As Variant
        ' Returns an array containing the unique values in r
        
        Dim cell        As Range
    
    #If EarlyBound Then
        ' Requires a reference to Microsoft Scripting Runtime
        With New Scripting.Dictionary
    #Else
        ' No reference required
        With CreateObject("Scripting.Dictionary")
    #End If
    
            For Each cell In r
                If Not (.Exists(cell.Value) Or IsEmpty(cell.Value)) Then
                    .Add Key:=cell.Value, Item:=vbNullString
                End If
            Next cell
        
            If bCount Then Unique = .Count Else Unique = .Keys
        End With
    End Function
    E.g., =CatUnique(A1:A10, "+")
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    03-21-2010
    Location
    Dorset, england
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: Want to stop my concatenate function concatenating duplicates

    Thanks guys both of your suggestions worked

+ 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