+ Reply to Thread
Results 1 to 3 of 3

How to pass dictionary from function into main sub?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    How to pass dictionary from function into main sub?

    Hello,

    my main sub is looking like that:

    Sub download_zlicz()
    
    Dim PathFiles As String
    Dim Variable As Integer
    Dim Array_Paths As Variant
    Dim i As Integer
    Dim wbworkbook As Workbook
    Dim dictionar As Object
    
    Set dictionar = CreateObject("Scripting.dictionary")
    
    Variable = 3
    
    Array_Paths = MsoFilePicker("Wybierz pliki", PathFiles, Variable)
    
        For i = 1 To UBound(Array_Paths)
            Set wbworkbook = Workbooks.Open(Array_Paths(i))
            Call WorkBooks_Looping(dictionar)
        Next i
    
    End Sub
    In bold code there is reference to function:

    Function WorkBooks_Looping(dictionar)
    
    Dim wbMain As Workbook
    Dim wsCount As Integer
    Set wbMain = ActiveWorkbook
    Dim wbworkbook As Worksheet
    Dim y As Long
    Dim i As Long
    Dim ArrayLoop As Variant
    Dim Dict_People As Object
    Dim coll As New Collection
    Dim Dictionary_Child As Object
    Dim varArray As Variant
    Dim Wylicz As String
    Dim z As Long
    Dim Count_Dict As Long
    Dim Dict_Temp As Object
    
    Set Dictionary_Child = CreateObject("Scripting.dictionary")
    wsCount = wbMain.Worksheets.Count
    
    Set wbworkbook = ActiveSheet
    
    For i = 1 To wsCount
    If wbMain.Worksheets(i).Name Like "20*" Then
    
    Set wbworkbook = ActiveSheet
    
    Set Dict_People = CreateObject("Scripting.dictionary")
    ArrayLoop = wbworkbook.Range(Cells(2, 5), Cells(154, 10))
    
    For y = 1 To UBound(ArrayLoop)
    
        If Len(ArrayLoop(y, 3)) > 1 Then
    
            If Not Dict_People.Exists(ArrayLoop(y, 3)) Then
            
                Dictionary_Child.Add ArrayLoop(y, 3), Nothing
                Dictionary_Child.Add ArrayLoop(y, 1), Nothing
                Dictionary_Child.Add ArrayLoop(y, 2), Nothing
                Dictionary_Child.Add ArrayLoop(y, 6), Nothing
                Dict_People.Add ArrayLoop(y, 3), Dictionary_Child
                
                Dim vitems, vkeys As Variant
                
                Set Dictionary_Child = Nothing
                Set Dictionary_Child = CreateObject("Scripting.dictionary")
                
                vkeys = Dict_People.Keys
                vitems = Dict_People.items
            Else
                
                Set Dict_Temp = CreateObject("Scripting.dictionary")
                
                Dict_Temp.Add ArrayLoop(y, 3), Nothing
                
                On Error Resume Next
                Dict_Temp.item(ArrayLoop(y, 3)) = Dict_People(ArrayLoop(y, 3)).Keys
                
                vkeys = Dict_Temp.Keys
                vitems = Dict_Temp.items
                varArray = Application.Index(Dict_Temp.items, 0, 0)
                varArray(4) = ArrayLoop(y, 6) + varArray(4)
                Dict_People.item(ArrayLoop(y, 3)) = varArray
                
                vkeys = Dict_People.Keys
                vitems = Dict_People.items
                
                Set Dict_Temp = Nothing
                
            End If
            
        End If
    
    Next y
    
    End If
    Next i
    
    WorkBooks_Looping = Dict_People
    
    End Function
    So after different countings I have dictionary Dict_People and i want to pass it into my main sub.

    Now function WorkBooks_Looping will be empty.

    How can i do this?

    Please help,
    Best Wishes,
    Jacek Antek

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to pass dictionary from function into main sub?

    Hi,

    You need to use Set with an object assignment
    Set WorkBooks_Looping = Dict_People
    It appears you want to populate dictionar with the result, so
    Sub download_zlicz()
    
    Dim PathFiles As String
    Dim Variable As Integer
    Dim Array_Paths As Variant
    Dim i As Integer
    Dim wbworkbook As Workbook
    Dim dictionar As Object
    
    Set dictionar = CreateObject("Scripting.dictionary")
    
    Variable = 3
    
    Array_Paths = MsoFilePicker("Wybierz pliki", PathFiles, Variable)
    
        For i = 1 To UBound(Array_Paths)
            Set wbworkbook = Workbooks.Open(Array_Paths(i))
            set dictionar = WorkBooks_Looping
        Next i
    End Sub
    and the function is
    Function WorkBooks_Looping() As Object
    
    Dim wbMain As Workbook
    Dim wsCount As Integer
    Set wbMain = ActiveWorkbook
    Dim wbworkbook As Worksheet
    Dim y As Long
    Dim i As Long
    Dim ArrayLoop As Variant
    Dim Dict_People As Object
    Dim coll As New Collection
    Dim Dictionary_Child As Object
    Dim varArray As Variant
    Dim Wylicz As String
    Dim z As Long
    Dim Count_Dict As Long
    Dim Dict_Temp As Object
    
    Set Dictionary_Child = CreateObject("Scripting.dictionary")
    wsCount = wbMain.Worksheets.Count
    
    Set wbworkbook = ActiveSheet
    
    For i = 1 To wsCount
    If wbMain.Worksheets(i).Name Like "20*" Then
    
    Set wbworkbook = ActiveSheet
    
    Set Dict_People = CreateObject("Scripting.dictionary")
    ArrayLoop = wbworkbook.Range(Cells(2, 5), Cells(154, 10))
    
    For y = 1 To UBound(ArrayLoop)
    
        If Len(ArrayLoop(y, 3)) > 1 Then
    
            If Not Dict_People.Exists(ArrayLoop(y, 3)) Then
            
                Dictionary_Child.Add ArrayLoop(y, 3), Nothing
                Dictionary_Child.Add ArrayLoop(y, 1), Nothing
                Dictionary_Child.Add ArrayLoop(y, 2), Nothing
                Dictionary_Child.Add ArrayLoop(y, 6), Nothing
                Dict_People.Add ArrayLoop(y, 3), Dictionary_Child
                
                Dim vitems, vkeys As Variant
                
                Set Dictionary_Child = Nothing
                Set Dictionary_Child = CreateObject("Scripting.dictionary")
                
                vkeys = Dict_People.Keys
                vitems = Dict_People.items
            Else
                
                Set Dict_Temp = CreateObject("Scripting.dictionary")
                
                Dict_Temp.Add ArrayLoop(y, 3), Nothing
                
                On Error Resume Next
                Dict_Temp.item(ArrayLoop(y, 3)) = Dict_People(ArrayLoop(y, 3)).Keys
                
                vkeys = Dict_Temp.Keys
                vitems = Dict_Temp.items
                varArray = Application.Index(Dict_Temp.items, 0, 0)
                varArray(4) = ArrayLoop(y, 6) + varArray(4)
                Dict_People.item(ArrayLoop(y, 3)) = varArray
                
                vkeys = Dict_People.Keys
                vitems = Dict_People.items
                
                Set Dict_Temp = Nothing
                
            End If
            
        End If
    
    Next y
    
    End If
    Next i
    
    Set WorkBooks_Looping = Dict_People
    
    End Function
    It is not (yet) clear what you intend to do with dictionar inside the loop.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: How to pass dictionary from function into main sub?

    Thank you xlnitwit.

    This is working.

    In function I am assigning dictionary to dictionary and sometimes if Key exists I am changing specific value within dictionary.

    Best Wishes,
    Jacek Antek

+ 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. Syntax/method to pass an AutoFilter criteria to a Function and use the value in function
    By excelnoobee in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-04-2016, 02:09 PM
  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. Storing a Dictionary into a Dictionary Object
    By pago_boss in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-16-2015, 03:04 AM
  4. Dictionary - Using a dictionary of dictionaries to hold individual orders
    By wazimu13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-09-2015, 08:32 PM
  5. Replies: 3
    Last Post: 06-07-2012, 07:25 PM
  6. [SOLVED] Analyze Within An Array and Pass Back Once vs. Pass at each applicable Instance
    By AlvaroSiza in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-11-2012, 05:25 PM
  7. Pass switch to Main procedure
    By Tod in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2005, 07:05 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