+ Reply to Thread
Results 1 to 8 of 8

Multiple Dictionaries Or One Dictionary

Hybrid View

goss Multiple Dictionaries Or One... 12-14-2012, 01:44 PM
mike7952 Re: Multiple Dictionaries Or... 12-14-2012, 01:50 PM
goss Re: Multiple Dictionaries Or... 12-14-2012, 03:17 PM
mike7952 Re: Multiple Dictionaries Or... 12-14-2012, 05:41 PM
goss Re: Multiple Dictionaries Or... 12-15-2012, 07:47 AM
jindon Re: Multiple Dictionaries Or... 12-14-2012, 07:04 PM
goss Re: Multiple Dictionaries Or... 12-15-2012, 07:49 AM
jindon Re: Multiple Dictionaries Or... 12-15-2012, 07:58 AM
  1. #1
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Multiple Dictionaries Or One Dictionary

    Hi all,

    Using Excel 2007.

    I have 4 lists Parts, Category, Country, Class
    I need to load a string, split the substring into array, loop the array to search dictionary(ies) to assign each element to the correct array.

    What would be best approach in term of the dictionary(ies)

    Thx
    w
    Kind regards,
    w

    http://dataprose.org

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Multiple Dictionaries Or One Dictionary

    Would be better to upload a wb and to show your expected results.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: Multiple Dictionaries Or One Dictionary

    Thanks Mike,

    Can I add a dictionary to an array
    I'm getting a subscript out of range error
    Seem like the code should work

    Thanks
    w

    Option Explicit
    Option Base 1
    
    
    Sub LoadDictionaries()
        '
        'Purpose:
        'Load Dictionaries
        '
        'References:
        '---------------------------------------------
        'Tools >> References >> Microsoft Scripting Runtime
        
        'Resources:
        '---------------------------------------------
        '
        'Date         Developer       Action
        '---------------------------------------------
        '12/14/2012   ws              Created
    
        'Initilialize
         With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
            .DisplayAlerts = False
         End With
         
        'Declare variables
         Dim wb As Workbook
         Dim ws As Worksheet
         Dim dic() As Dictionary
         Dim i As Integer, j As Integer, k As Integer, x As Integer, y As Integer
         Dim lRows As Long
         
        'Intialize variables
         x = 1
        
        'Object reference
         Set wb = ThisWorkbook
         
        'Count number of sheets with dictionary entries
         For Each ws In wb.Worksheets
            If Left$(ws.Name, 1) = "d" Then
                i = i + 1
            End If
         Next ws
         
        'Load dictionaries from worksheets
        'Assume data starts in $A$1 on each worksheet
        'In the dictionary pairs below, the key comes first, the item comes second
        
         For Each ws In wb.Worksheets
            If Left$(ws.Name, 1) = "d" Then
                lRows = ws.Cells(Rows.Count, 1).End(xlUp).Row 'Last row
                Set dic(x) = New Dictionary
                    For k = 1 To lRows
                        dic(x).Add ws.Cells(k, 1), ws.Cells(k, 2)
                    Next k
                x = x + 1
            End If
         Next ws
                        
         For y = 1 To x
            Debug.Print dic(y).Count;
         Next y
    
    
    'Tidy up
        'Erase arrays
         Erase dic()
         
        'Destroy objects
         Set wb = Nothing
    
        'Excel environment
         With Application
            .ScreenUpdating = True
            .Calculation = xlCalculationAutomatic
            .DisplayAlerts = True
         End With
         
    End Sub
    Last edited by goss; 12-14-2012 at 03:22 PM. Reason: typo

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Multiple Dictionaries Or One Dictionary

    Give this a try

    Option Explicit
    Option Base 1
    
    
    Sub LoadDictionaries()
        '
        'Purpose:
        'Load Dictionaries
        '
        'References:
        '---------------------------------------------
        'Tools >> References >> Microsoft Scripting Runtime
        
        'Resources:
        '---------------------------------------------
        '
        'Date         Developer       Action
        '---------------------------------------------
        '12/14/2012   ws              Created
    
        'Initilialize
         With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
            .DisplayAlerts = False
         End With
         
        'Declare variables
         Dim wb As Workbook
         Dim ws As Worksheet
         Dim dic As Scripting.Dictionary
         Dim i As Long, j As Long, k As Long, y As Long
         Dim lRows As Long
         Dim x As Variant
         
        'Intialize variables
         x = 1
        
        'Object reference
         Set wb = ThisWorkbook
         
        'Count number of sheets with dictionary entries
         For Each ws In wb.Worksheets
            If Left$(ws.Name, 1) = "d" Then
                i = i + 1
            End If
         Next ws
         
        'Load dictionaries from worksheets
        'Assume data starts in $A$1 on each worksheet
        'In the dictionary pairs below, the key comes first, the item comes second
         Set dic(x) = New Scripting.Dictionary 'Set your dictionary ouside the loop
         For Each ws In wb.Worksheets
            If Left$(ws.Name, 1) = "d" Then
                lRows = ws.Cells(Rows.Count, 1).End(xlUp).Row 'Last row
                For k = 1 To lRows
                    dic(ws.Cells(k, 1)) = ws.Cells(k, 2)
                Next k
            End If
         Next ws
         
         x = dic.Items
         For y = 0 To UBound(x)
            Debug.Print x(y)
         Next y
    
    
    'Tidy up
        'Erase arrays
    
         
        'Destroy objects
         Set wb = Nothing
    
        'Excel environment
         With Application
            .ScreenUpdating = True
            .Calculation = xlCalculationAutomatic
            .DisplayAlerts = True
         End With
         
    End Sub

  5. #5
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: Multiple Dictionaries Or One Dictionary

    Thanks Mike,

    That does not appear to add multiple dictionaries, which is fine - except, how do I know the class of an item if I do not know which dictionary it cam from?
    My thought was
    • Grab element from array avItem(j,i)
      Search dic(1) if found then item is in Class Country
      Assign to avCountry()
      Else search dic(2)
      If found then item is in class Customer
      Assign to avCustomer()
      Else search dic(3)
      If found then item is in class Part
      Assign to avPart()
      Else search dic(4)
      If found then item is in class Date
      Assign to avDate()
      Else
      Not found print avItem(j,i) #FileNum

    Is that doable?
    Is there a better way?
    thx
    w

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

    Re: Multiple Dictionaries Or One Dictionary

    See here for reference.

    Dictionary on Dictionary.

  7. #7
    Forum Contributor
    Join Date
    01-07-2004
    Posts
    314

    Re: Multiple Dictionaries Or One Dictionary

    Thanks Jindon,

    It will take me some time to digest your code.
    I've used VBA for awhile, I've never used the dictionary object.

    thx
    w

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

    Re: Multiple Dictionaries Or One Dictionary

    Give you the idea.
         Dim dic As Object, ws As worksheet, i as long
         Set dic = Createobject("Scripting.Ditionary")
         For Each ws In wb.Worksheets
            If Left$(ws.Name, 1) = "d" Then
    	    Set dic(ws.Name) = Createobject("Scripting.Ditionary") '<- dictionary on dictionay
                For i = 1 To ws.Cells(Rows.count, 1).End(xlUp).Row
                    dic(ws.Name)(ws.Cells(i, 1).Value) = ws.Cells(i, 2).Value
                Next
            End If
         Next ws
    So, Main dictionary holds worksheet.names and each item holds its own Dictionary.
    Last edited by jindon; 12-15-2012 at 08:00 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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