+ Reply to Thread
Results 1 to 3 of 3

how to pass array values to function

  1. #1
    Registered User
    Join Date
    12-08-2008
    Location
    US
    Posts
    14

    how to pass array values to function

    Hello,

    I have a simple sub that generates an array:

    Please Login or Register  to view this content.
    Although it is likely syntactically incorrect, I am trying to do something like the above, where the function can call in some elements of the array created in the sub that called it and process some function of those elements, then pass the output back up into the sub.

    I could call it like gen1(n) = genreturn(gen(1), gen(2), gen(3)) but that way would seem painfully long. I could also generate the numbers inside of the function each time, but that would seem incredulously inefficient.
    I basically want a way to pass partial array elements generated in the sub to be processed in the function and passed back up. Any ideas?
    Last edited by exceltools2; 12-10-2008 at 08:57 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello exceltools2,

    You can pass any number of elements and types to another Sub or Function by using the ParamArray structure. This is from the VBA help files.

    A parameter array can be used to pass an array of arguments to a procedure. You don't have to know the number of elements in the array when you define the procedure.

    You use the ParamArray keyword to denote a parameter array. The array must be declared as an array of type Variant, and it must be the last argument in the procedure definition.

    The following example shows how you might define a procedure with a parameter array.
    Please Login or Register  to view this content.
    The following examples show how you can call this procedure.

    AnyNumberArgs "Jamie", 10, 26, 32, 15, 22, 24, 16
    AnyNumberArgs "Kelly", "High", "Low", "Average", "High"

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    12-08-2008
    Location
    US
    Posts
    14
    Thank You,

    I tried something like the following:

    Please Login or Register  to view this content.
    It accepts the paramarray argument, but has a type error on the sumtest + argument. Also, ideally I would like output gen to call some range of inputgen elements, like
    Please Login or Register  to view this content.
    Where the function would then process those elements from inputgen 2 through 11 in a loop of each element, one at a time.

    is there a way to do this specific example? thanks again.
    -----------------------------------------------------------------------
    -----------------------------------------------------------------------
    An example of something closer that works is as follows:
    Please Login or Register  to view this content.
    Only, I had to explicitly call each element in my function:
    i.e. out(i) = outputgen(inputgen(1), inputgen(2), inputgen(3))

    I would like to simply say out(i) = outputgen(inputgen())
    or out(i) = outputgen(inputgen(1:3))
    instead of listing each element explicitly.
    Ideally I could just call a function like outputgen(nfirst, nlast, inputgen())
    and then have n be the number of elements of inputgen to process.

    Any other ideas?
    Last edited by exceltools2; 12-10-2008 at 10:09 PM.

+ 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