+ Reply to Thread
Results 1 to 3 of 3

how to pass array values to function

Hybrid View

  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:

    sub array_gen()
    
    dim gen(10),  gen1(10) as variant
    
    for i = 1 to 10
    gen(i) = i
    next i
    
    For n = 1 to 10
    gen1(n) = genreturn(gen(n))
    next i
    
    end sub
    
    Function genreturn(array(i) as variant) as variant
    
    sum = 0
    
    For i = 1 to 10
    sum = sum + array(i)
    next i
    
    genreturn(i) = sum
    
    End Function
    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,259
    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.
    Sub AnyNumberArgs(strName As String, ParamArray intScores() As Variant)
        Dim intI As Integer
    
        Debug.Print strName; "    Scores"
        ' Use UBound function to determine upper limit of array.
        For intI = 0 To UBound(intScores())
            Debug.Print "          "; intScores(intI)
        Next intI
    End Sub
    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:

    Sub test_sums()
    
    Dim inputgen(10), out(10) As Variant
    
    For i = 1 To 10
    inputgen(i) = i
    Next i
    
    
    For i = 1 To 10
    out(i) = outputgen(inputgen())
    Next i
    
    End Sub
    
    Function outputgen(ParamArray inputgen() As Variant)
    
    sumtest = 0
    
    For i = 1 To UBound(inputgen())
    sumtest = sumtest + inputgen()
    Next i
    
    End Function
    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
    out(i) = outputgen(inputgen(2:11))
    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:
    Sub test_sums()
    
    Dim inputgen(10), out(10) As Variant
    
    For i = 0 To 9
    inputgen(i) = i + 5
    Next i
    
    
    For i = 1 To 10
    out(i) = outputgen(inputgen(1), inputgen(2), inputgen(3))
    Next i
    
    End Sub
    
    Function outputgen(ParamArray inputgen() As Variant)
    Dim sumtest As Double
    
    
    
    sumtest = 0
    
    For i = 0 To UBound(inputgen())
    sumtest = inputgen(i) + sumtest
    Next i
    
    
    
    outputgen = sumtest
    
    End Function
    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