+ Reply to Thread
Results 1 to 10 of 10

Excel VBA Code Instead of SumIf Array Formula for Unique Values

Hybrid View

  1. #1
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Excel VBA Code Instead of SumIf Array Formula for Unique Values

    Hello excelforumkeys,

    The macro below has been added to the attached workbook and is called by the command button. Placing the count next to each id is what slows the operation to a crawl. To speed things up I have the unique list and counts output to "Sheet2". All the data values are moved into RAM by using arrays. This eliminates the overhead encountered with Range objects. Try this out and let me know the results.

    Sub Macro1()
    
        Dim Data    As Variant
        Dim DstWks  As Worksheet
        Dim Cnt     As Long
        Dim Key     As Variant
        Dim LastRow As Long
        Dim Rng     As Range
        Dim SrcWks  As Worksheet
        Dim Sums    As Object
       
            Set SrcWks = Worksheets("Sheet1")
            Set DstWks = Worksheets("Sheet2")
            
            Set Rng = SrcWks.Range("A2:B2")
            LastRow = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp).Row
            If LastRow < Rng.Row Then Exit Sub Else Set Rng = Rng.Resize(LastRow - Rng.Row + 1, 2)
            
                Set Sums = CreateObject("Scripting.Dictionary")
                Sums.CompareMode = vbCompare
                
                Data = Rng.Value
                
                For i = 1 To LastRow - Rng.Row + 1
                    Key = Data(i, 1) & "|" & Data(i, 2)
                    If Not Sums.Exists(Key) Then
                        Sums.Add Key, 1
                    Else
                        Cnt = Sums(Key)
                        Sums(Key) = Cnt + 1
                    End If
                Next i
                
                Cnt = 0
                ReDim Data(1 To Sums.Count, 1 To 3)
                
                For Each Key In Sums.Keys
                    Cnt = Cnt + 1
                    i = InStr(1, Key, "|")
                    Data(Cnt, 1) = Left(Key, i - 1)
                    Data(Cnt, 2) = Right(Key, Len(Key) - i)
                    Data(Cnt, 3) = Sums(Key)
                Next Key
                    
            Application.ScreenUpdating = False
                DstWks.UsedRange.Offset(1, 0).ClearContents
                DstWks.Cells(Rng.Row, Rng.Column).Resize(Cnt, 3).Value = Data
            Application.ScreenUpdating = True
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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. [SOLVED] Array formula to extract and sort unique values from two worksheets
    By rshukla in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-07-2013, 06:11 AM
  2. add to array formula to only return unique values
    By jason892 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-11-2013, 06:39 AM
  3. [SOLVED] Count unique values in list but NOT using ARRAY formula
    By alx0101 in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 03-15-2013, 08:15 AM
  4. Replies: 3
    Last Post: 11-24-2011, 06:11 AM
  5. Replies: 6
    Last Post: 06-14-2011, 07:18 AM

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