+ Reply to Thread
Results 1 to 5 of 5

Speeding Up Unique Value List Function

Hybrid View

  1. #1
    Registered User
    Join Date
    03-25-2013
    Location
    CT, USA
    MS-Off Ver
    Excel 2010
    Posts
    2

    Exclamation Speeding Up Unique Value List Function

    Hi all,

    Long time visitor, first time poster. I was able to create this following Frankenstein function that will take in an array or range and pick out the unique values, then enter them into a comma separated list. I created it from various UDFs I've seen on this site and elsewhere. This function needs to be able to take arrays, not just ranges, as I use the IF function extensively within the UDF.

    The problem I am having with this is that running this function about 2000 times will take over an hour to run. I don't believe the code is properly optimized and would appreciate any help from some of the VBA experts on this site. Let me know if I can provide any more details that might be helpful.

    Thanks.

    Function UniqueIf(MyArray As Variant, Optional ByVal iFuncNum As Integer = 1, Optional ByVal sDelim As String = ", ") As Variant
    
        Dim oDict As Object
        Dim sTxt As String
        Dim i As Long
        Dim j As Long
        Dim nElements As Long
        Dim tElements() As Variant
        Dim nUnique As Long
    
    
        Set oDict = CreateObject("Scripting.Dictionary")
        With oDict
    
        nElements = UBound(MyArray)
    
        ReDim tElements(1 To nElements)
    
        nUnique = 0 'To correctly enter first unique value when nUnique is increased by 1 below
    
        For i = 1 To nElements
            
        If MyArray(i, 1) = "" Then
        'Exit For
        GoTo NextIteration
        End If
            
            If MyArray(i, 1) > "" Then
                    
                    For j = 1 To nUnique
                        If tElements(j) = MyArray(i, 1) Then
                    Exit For
                        End If
            
                    Next j
                        
                If j > nUnique Then
                    nUnique = nUnique + 1
                    tElements(nUnique) = MyArray(i, 1)
                        
    '                .Add rCell.Text, rCell.Text
                    sTxt = sTxt & sDelim & MyArray(i, 1)
            End If
        End If
    
    NextIteration:  Next i
        End With
        
        If iFuncNum = 1 Then
            UniqueIf = Mid(sTxt, Len(sDelim) + 1)
        ElseIf iFuncNum = 2 Then
            UniqueIf = nUnique
        Else
            UniqueIf = CVErr(xlErrValue)
        End If
    
    
    End Function

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Speeding Up Unique Value List Function

    ibtheib,

    Welcome to the forum!
    Give this a try:
    Public Function UniqueItems(ByVal varData As Variant, _
                                Optional ByVal lFuncNum As Long = 1, _
                                Optional ByVal sDelim As String = ", ") As Variant
        
        Dim cllUnique As Collection
        Dim varItem As Variant
        Dim strUnq As String
        Dim i As Long
        
        Set cllUnique = New Collection
        
        On Error Resume Next
        For Each varItem In varData
            cllUnique.Add varItem, varItem
            If cllUnique.Count > i Then
                i = cllUnique.Count
                strUnq = strUnq & sDelim & varItem
            End If
        Next varItem
        On Error GoTo 0
        
        Select Case lFuncNum
            Case 1: UniqueItems = Mid(strUnq, Len(sDelim) + 1)
            Case 2: UniqueItems = cllUnique.Count
        End Select
        
        Set cllUnique = Nothing
        
    End Function
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

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

    Re: Speeding Up Unique Value List Function

    Is this how you wanted?
    Function UniqueIf(MyArray As Variant, Optional ByVal iFuncNum As Integer = 1, _
            Optional ByVal sDelim As String = ", ") As Variant
    
        Dim oDict As Object, e
        Set oDict = CreateObject("Scripting.Dictionary")
        For Each e In MyArray
            If e <> "" Then oDict(e) = Empty
        Next
        UniqueIf = IIf(iFuncNum = 1, Join(oDict.keys, sDelim), oDict.Count)
    End Function

  4. #4
    Registered User
    Join Date
    03-25-2013
    Location
    CT, USA
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Speeding Up Unique Value List Function

    Thanks a lot. Both of these are improvements on what I was using. jindon's solution was better, as it took 10 minutes to run for the entire sheet, and the VBA code is just so simple.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Speeding Up Unique Value List Function

    or

    Function unique_snb(c00)
      with createobject("scripting.dictionary")
        for each it in c00
          x=.item(it)
        next
    
        unique_snb=join(.keys,",")
      end with
    End Fuction



+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Help speeding up matching a list of values over multiple workbooks
    By wizuriel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2013, 12:09 PM
  2. [SOLVED] Function to populate a list of unique values
    By Sophie.Durrant in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-08-2013, 10:57 AM
  3. Speeding up a custom lookup function
    By Tsjallie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2012, 05:44 PM
  4. use max function on a list of unique items
    By jcavigli in forum Excel General
    Replies: 3
    Last Post: 07-06-2009, 05:16 PM
  5. [SOLVED] Function to count unique items in list
    By XP in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-10-2006, 01:35 PM

Tags for this Thread

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