Results 1 to 10 of 10

Excel VBA Code Instead of SumIf Array Formula for Unique Values

Threaded View

  1. #4
    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 Chris,

    Sorry, I misunderstood what you wanted. This workbook produces the output you showed in the picture. The new code is shown below and has been added to the attached workbook.

    Sub Macro2()
    
      ' Get sums for unique products in each account.
      
        Dim Account As Variant
        Dim Data    As Variant
        Dim DstWks  As Worksheet
        Dim Cnt     As Long
        Dim LastRow As Long
        Dim Products As String
        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
                    Account = Data(i, 2)
                    If Not Sums.Exists(Account) Then
                        Products = Data(i, 1)
                        Sums.Add Account, Products
                    Else
                        Products = Sums(Account)
                        Cnt = InStr(1, Products, Data(i, 1))
                        If Cnt = 0 Then
                            Sums(Account) = Products & "|" & Data(i, 1)
                        End If
                    End If
                Next i
                
                Cnt = 0
                ReDim Data(1 To Sums.Count, 1 To 2)
                
                For Each Key In Sums.Keys
                    Cnt = Cnt + 1
                    Data(Cnt, 1) = Key
                    Data(Cnt, 2) = UBound(Split(Sums(Key), "|")) + 1
                Next Key
                    
            Application.ScreenUpdating = False
                DstWks.UsedRange.Offset(1, 0).ClearContents
                DstWks.Cells(Rng.Row, Rng.Column).Resize(Cnt, 2).Value = Data
            Application.ScreenUpdating = True
            
    End Sub
    Attached Files Attached Files

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