dcsimg
Results 1 to 4 of 4

Thread: Excel VBA: How To: Pass Array to nested function ???

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Question Excel VBA: How To: Pass Array to nested function ???

    Esteemed Forum Participants and Lurkers:
    ===============================
    Excel 2003 VBA

    I can't remember how to pass an array to a nested function. This is a snippet ... I can do everything except I can't seem to get the dictionary arrays passed through the "TopFunction" into the "DictionaryProcessor" function:
    Code:
    Sub JUNK()
    dim aDictionary(3) as New Dictionary
    
        'Load up the Dictionaries here
        Call TopFunction(??? pass the dictionary stuff here by Reference ???)
    
    End Sub
    
    Function TopFunction(??? receive the dictionary stuff here by Reference ???)
    
        'Prepare a Prefix for the dictionaries and output to a text file ...
        Call DictionaryProcessor(??? pass the dictionary stuff here by Reference ???)
    
    End Function
    
    Function DictionaryProcessor(aDictionary() as Dictionary)
    
        'Output the Dictionaries in a calculated sequence
        '    aDictionary(2)
        '    aDictionary(3)
        '    aDictionary(0)
        '    aDictionary(1)
    
    End Function
    Thank you for any and all comments, suggestions, and assistance.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,847

    Re: Excel VBA: How To: Pass Array to nested function ???

    you could declare the dictionary at global level, then you don't need to pass it
    this may or may not be suitable
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Excel VBA: How To: Pass Array to nested function ???

    Code:
    public sub dostuff(byref ary() as dictionary)
    
    end sub
    
    public sub testme()
        dim aryD(3) as new dictionary
    
       dostuff(aryD())
    
    end sub
    Think that will do it - post back if not....
    Last edited by Ecniv; Nov 21st, 2008 at 03:29 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Excel VBA: How To: Pass Array to nested function ???

    Thanks everyone ...

    If I had spent as much time trying to hack through the problem as I did writing the letter to the Forum, I would have solved it. I was in a hurry to leave to go home. Here is the final solution:
    Code:
    Option Explicit
    'Requires Reference to MS Scripting Runtime Library
    
    Sub SNIPPET()
        Dim aDictionary(2) As New dictionary
        aDictionary(0).Add "000", "This is Dictionary 1"
        aDictionary(1).Add "001", "This is Dictionary 2"
        aDictionary(2).Add "002", "This is Dictionary 3"
        Call TopProcess(aDictionary)   'WORKS!
        Call TopProcess(aDictionary()) 'WORKS!
    End Sub
    
    Function TopProcess(aDictionary() As dictionary)
        Call ProcessDictionaries(aDictionary)   'WORKS!
        Call ProcessDictionaries(aDictionary()) 'WORKS!
    End Function
    
    Function ProcessDictionaries(aDictionary() As dictionary)
        Debug.Print aDictionary(0)("000")
        Debug.Print aDictionary(1)("001")
        Debug.Print aDictionary(2)("002")
    End Function
    Thanks to all, and regards ...
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width