+ Reply to Thread
Results 1 to 6 of 6

Referencing multiple dic.items in dictionary to clean up my code

Hybrid View

strud Referencing multiple... 10-15-2013, 05:38 AM
nilem Re: Referencing multiple... 10-15-2013, 05:44 AM
strud Re: Referencing multiple... 10-15-2013, 06:25 AM
MickG Re: Referencing multiple... 10-15-2013, 07:44 AM
strud Re: Referencing multiple... 10-15-2013, 07:56 AM
MickG Re: Referencing multiple... 10-15-2013, 07:58 AM
  1. #1
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Referencing multiple dic.items in dictionary to clean up my code

    I am currently using two dictionaries in one code, which works, but was wandering whether the code could be cleaned up so as to execute the same series of events using only one dictionary. I prefer to keep codes to a minimum if I can.

    Essentially, the code checks one sheet for a product code. It then 'copies' the product code and data from two specific columns in the same row. If there are multiple of the same product code it will accumulate the data in the other two columns the 'end product' is just one row with the product code and accumulated totals adjacent.

    Here is the code....what I am trying to do would be combine dic and dic2, but I cannot work out how...

    Sub forforum()
    
    Dim Orders As Variant
    Dim i As Long
    Dim dic As Object
    Dim dic2 As Object
    Dim Criteria As Variant
    Dim Criteria2 As Date
    Dim Criteria3 As Date
    Dim Quantity As Variant
    Dim Code As String
    Dim Keys As Variant
    Dim OrderDate As Date
    Dim Subtotal As Variant
    
    Application.ScreenUpdating = False
    Criteria = Sheets("Sheet2").Range("D3").Value2
    Criteria2 = Sheets("Sheet2").Range("D4").Value2
    Criteria3 = Sheets("Sheet2").Range("D5").Value2
    
        With Sheets("Sheet1")
        
            Orders = .Range("a3:j" & .Cells(.Rows.Count, "d").End(xlUp).Row).Value
        
            Set dic = CreateObject("Scripting.Dictionary")
                For i = 1 To UBound(Orders, 1)
                    Code = Orders(i, 8)
                    Quantity = Orders(i, 9)
                    OrderDate = Orders(i, 4)
                    
                If Code = Criteria And OrderDate >= Criteria2 And OrderDate <= Criteria3 Then
                    If dic.Exists(Code) Then
                    
                    dic.Item(Code) = dic.Item(Code) + Quantity
                    
                    Else
                    dic.Add Code, Quantity
                    End If
                End If
                Next
        End With
        
        With Sheets("Sheet2")
            Keys = dic.Keys
            If dic.Count Then
                .Range("f2").Resize(dic.Count, 2).Value = Application.Transpose(Keys)
                For i = 0 To UBound(Keys)
                .Cells(i + 2, "g") = dic.Item(Keys(i))
                Next
            End If
      End With
        
        With Sheets("Sheet1")
        
        Orders = .Range("a3:j" & .Cells(.Rows.Count, "d").End(xlUp).Row).Value
        
         Set dic2 = CreateObject("Scripting.Dictionary")
                For i = 1 To UBound(Orders, 1)
                    Code = Orders(i, 8)
                    Quantity = Orders(i, 9)
                    Subtotal = Orders(i, 10) * Orders(i, 9)
                    OrderDate = Orders(i, 4)
                    
                If Code = Criteria And OrderDate >= Criteria2 And OrderDate <= Criteria3 Then
                    If dic2.Exists(Code) Then
                    
                    dic2.Item(Code) = dic2.Item(Code) + Subtotal
                    
                    Else
                    dic2.Add Code, Subtotal
                    End If
                End If
                Next
        End With
        
        With Sheets("Sheet2")
            Keys = dic2.Keys
            If dic.Count Then
                .Range("h2").Resize(dic.Count, 1).Value = Application.Transpose(dic2.Items)
            End If
        End With
        
    
    Set dic = Nothing
    Set dic2 = Nothing
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Referencing multiple dic.items in dictionary to clean up my code

    Hi Strud
    can you upload an example workbook

  3. #3
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: Referencing multiple dic.items in dictionary to clean up my code

    Sure...here you go..

    I have omitted irrelevant data so ignore blank columns
    Attached Files Attached Files

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Referencing multiple dic.items in dictionary to clean up my code

    Perhaps this:-
    Sub MG15Oct41
    Dim Orders As Variant
    Dim i As Long
    Dim dic As Object
    Dim dic2 As Object
    Dim Criteria As Variant
    Dim Criteria2 As Date
    Dim Criteria3 As Date
    Dim Quantity As Variant
    Dim Code As String
    Dim Keys As Variant
    Dim OrderDate As Date
    Dim SubTot As Double
    Dim Q As Variant
    Application.ScreenUpdating = False
    Criteria = Sheets("Sheet2").Range("D3").Value2
    Criteria2 = Sheets("Sheet2").Range("D4").Value2
    Criteria3 = Sheets("Sheet2").Range("D5").Value2
    
        With Sheets("Sheet1")
        
            Orders = .Range("a3:j" & .Cells(.Rows.Count, "d").End(xlUp).Row).Value
        
            Set dic = CreateObject("Scripting.Dictionary")
                For i = 1 To UBound(Orders, 1)
                    Code = Orders(i, 8)
                    Quantity = Orders(i, 9)
                    OrderDate = Orders(i, 4)
                    SubTot = Orders(i, 10)
                If Code = Criteria And OrderDate >= Criteria2 And OrderDate <= Criteria3 Then
                    If dic.Exists(Code) Then
                        Q = dic.Item(Code)
                         Q(0) = Q(0) + Quantity
                         Q(1) = Q(1) + Quantity * SubTot
                    dic.Item(Code) = Q
                    Else
                        dic.Add Code, Array(Quantity, Quantity * SubTot)
                    End If
                End If
                Next
        End With
        
        With Sheets("Sheet2")
            Keys = dic.Keys
            If dic.Count Then
                .Range("f2").Resize(dic.Count).Value = Application.Transpose(Keys)
                For i = 0 To UBound(Keys)
                    .Cells(i + 2, "g") = dic.Item(Keys(i))(0)
                    .Cells(i + 2, "H") = dic.Item(Keys(i))(1)
                Next
            End If
      End With
        
    
    Set dic = Nothing
    Application.ScreenUpdating = True
    End Sub
    Regards Mick

  5. #5
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: Referencing multiple dic.items in dictionary to clean up my code

    Brilliant Mick that's perfect!!

    I can see how you've referenced both items for one key:

    Q = dic.Item(Code)
                         Q(0) = Q(0) + Quantity
                         Q(1) = Q(1) + Quantity * SubTot
    So I'm with you.

    Thanks for your help...was curiosity really but I think it's good practice to try to keep codes as clean as possible.

    Thanks again

  6. #6
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Referencing multiple dic.items in dictionary to clean up my code

    Your welcome
    Regrds Mick

+ 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] Add multiple items to scripting dictionary from one key and keep structure
    By strud in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-07-2013, 09:44 AM
  2. Referencing Multiple Items from Pivot Field in macro
    By dgeisman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-07-2012, 01:01 PM
  3. [SOLVED] VBA Code to optimize and clean data- clean out numerical/ or symbol
    By tracylsr in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-05-2012, 04:21 PM
  4. Referencing multiple items
    By msbaath in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-02-2008, 03:52 PM
  5. looping through items in a dictionary
    By Abe in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-08-2006, 05:45 AM

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