+ Reply to Thread
Results 1 to 6 of 6

Scripting Dictionary for Sumif Statement

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-14-2012
    Location
    Davenport, Iowa
    MS-Off Ver
    Excel 365
    Posts
    206

    Scripting Dictionary for Sumif Statement

    Hi,

    I have a large data set and using a sumif formula take a while (4-5 Minutes) is there any chance this can be done via a scripting dictionary?
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: Scripting Dictionary for Sumif Statement

    I added the macro to your file. I added a reference to "Microsoft Scriping Runtime"
    Sub CollectData()
      
      Dim dict As New Dictionary
      Dim i As Long
      
      Dim vArr As Variant
      Dim Keys As Variant
      
      'read data into variant array
      vArr = ActiveSheet.Range("F1").CurrentRegion
      
      ' collect data below header till end
      For i = 2 To UBound(vArr, 1)
        If dict.Exists(CStr(vArr(i, 1))) Then
          dict.Item(CStr(vArr(i, 1))) = dict.Item(CStr(vArr(i, 1))) + CInt(vArr(i, 3))
        Else
          dict.Add CStr(vArr(i, 1)), CInt(vArr(i, 3))
        End If
      Next
        
     '--- test output ---
     ' copy summary data to sheet
      i = 5
      For Each Keys In dict.Keys
        i = i + 1
        ActiveSheet.Cells(i, 1) = Keys
        ActiveSheet.Cells(i, 2) = dict.Item(CStr(Keys))
      Next
    
    End Sub
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    12-14-2012
    Location
    Davenport, Iowa
    MS-Off Ver
    Excel 365
    Posts
    206

    Re: Scripting Dictionary for Sumif Statement

    Gue2013 - I get a user defined error when trying to declare the new Dictionary

  4. #4
    Valued Forum Contributor
    Join Date
    07-14-2017
    Location
    Poland
    MS-Off Ver
    Office 2010
    Posts
    528

    Re: Scripting Dictionary for Sumif Statement

    Check this code.
    Sub Sum_()
        Dim a()
        Dim i As Long, lr As Long
        Dim d As Object
        
        Set d = VBA.CreateObject("scripting.dictionary")
        With Sheets("Sheet1")
            lr = .Cells(Rows.Count, "F").End(3).Row
            .[A1].CurrentRegion.Offset(1, 0).ClearContents
            If lr > 1 Then
                a = .Range("F2:H" & lr).Value
                For i = 1 To UBound(a)
                    d(a(i, 1)) = IIf(IsEmpty(d(a(i, 1))), 0, d.Item(a(i, 1))) + a(i, 3)
                Next
                With .[A1].CurrentRegion.Offset(1, 0)
                    .Resize(d.Count, 1).Value = Application.Transpose(d.keys)
                    .Offset(0, 1).Resize(d.Count, 1) = Application.Transpose(d.items)
                End With
            End If
        End With
        Set d = Nothing
    End Sub
    Best Regards,
    Maras.

  5. #5
    Forum Contributor
    Join Date
    12-14-2012
    Location
    Davenport, Iowa
    MS-Off Ver
    Excel 365
    Posts
    206

    Re: Scripting Dictionary for Sumif Statement

    Maras_Mak - Works like a charm! Thank you!

  6. #6
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: Scripting Dictionary for Sumif Statement

    you most probably forgot to add the reference to "Microsoft Scriping Runtime"

+ 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. scripting dictionary
    By madhukar37 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-15-2018, 06:13 AM
  2. [SOLVED] Dictionary or Scripting.Dictionary. Binding Referencing Dim-ing. Sub routines and Function
    By Doc.AElstein in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-12-2016, 08:28 AM
  3. Scripting dictionary help
    By leanne2011 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-04-2016, 09:32 PM
  4. Scripting dictionary help
    By leanne2011 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-14-2014, 09:51 AM
  5. need help with scripting dictionary
    By leanne2011 in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 10-04-2014, 04:33 PM
  6. [SOLVED] Using Dictionary in VBA scripting
    By vnzerem in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-04-2014, 02:54 PM
  7. [SOLVED] Scripting Dictionary
    By thisisgerald in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-18-2012, 01:30 PM

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