+ Reply to Thread
Results 1 to 8 of 8

Multiple Dictionaries Or One Dictionary

Hybrid View

  1. #1
    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
    Thanks,
    Mike

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

  2. #2
    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
    Kind regards,
    w

    http://dataprose.org

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 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