Thanks Thanks:  0
Likes Likes:  0
Results 1 to 8 of 8

Thread: Passing an array between modules

  1. #1
    New Member
    Join Date
    Feb 2002
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    If I have declared an array

    Random_Data(1 To 15) As Integer

    How can I pass these values between modules? Can I call it in a subroutine?

    Thanks.

  2. #2
    MrExcel MVP DRJ's Avatar
    Join Date
    Feb 2002
    Location
    California
    Posts
    3,853
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi

    You can call the sub with the variables or you can globaly declare them.

    Option 1

    Sub MyCode()

    Dim MyArray(14) as Integer

    MyArray = something you set it to equal

    More Code

    Call MyCode2(MyArray)

    End Sub

    Sub MyCode2(NewArray(14) as Integer)

    More Code

    End Sub

    Or

    Option 2

    Dim MyArray(14) As Integer

    Sub MyCode

    More Code (MyArray is declared and will retain its values)

    Call MyCode2

    End Sub

    Sub MyCode2()


    More Code (MyArray is declared and will retain its values)

    End Sub


    HTH

    DRJ

  3. #3
    New Member
    Join Date
    Feb 2002
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    It did help, thank you! I couldn't get the former portion to work, but the latter worked just fine. Thanks again.

    DEW

  4. #4
    MrExcel MVP Mark O'Brien's Avatar
    Join Date
    Feb 2002
    Location
    Columbus, OH, USA
    Posts
    3,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default


    Sub MyCode2(NewArray(14) as Integer)
    Off of the top of my head, this would have failed because you need to pass arrays ByRef and not ByVal. You also couldn't specify the size and I believe maybe not even the typ of the array (my help file is knackered right now or else I'd look it up). I believe this code would work with something like:

    Sub MyCode2(ByRef NewArray() as Variant)


    It tends to be a bad idea to have any global variables in your program as this makes the program more susceptible to errors. Usually there is a way to pass the argument rather than to rely on Global variables.


    _________________
    [b] Mark O'Brien

    [ This Message was edited by: Mark O'Brien on 2002-02-22 14:44 ]

  5. #5
    MrExcel MVP Russell Hauf's Avatar
    Join Date
    Feb 2002
    Location
    Portland, OR Area - USA
    Posts
    1,609
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Mark is correct. And you can declare the type of the array in the argument list. In fact, it's a very good idea (unless you have an array with mixed types) - this way if you try to pass the wrong array to your function that takes an array, it won't work (and won't mess up your array or give an unexpected error). Here is a simple example:

    Code:
    Sub TestArray1()
        Dim arr(1 To 2) As Integer
        arr(1) = 4
        arr(2) = 8
        Call TestArray2(arr)
        Debug.Print arr(1) & "  " & arr(2)
    End Sub
    
    Sub TestArray2(arr() As Integer)
        Dim intI As Integer
        For intI = 1 To UBound(arr)
            arr(intI) = arr(intI) * 2
        Next intI
    End Sub
    You don't need to have the same name in both procedures/functions - I just used a short name.

    Hope this gives you a better understanding,

    Russell

  6. #6
    Board Regular
    Join Date
    May 2002
    Location
    mtl, canada
    Posts
    160
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    On 2002-02-22 15:11, Russell Hauf wrote:
    Mark is correct. And you can declare the type of the array in the argument list. In fact, it's a very good idea (unless you have an array with mixed types) - this way if you try to pass the wrong array to your function that takes an array, it won't work (and won't mess up your array or give an unexpected error). Here is a simple example:

    Code:
    Sub TestArray1()
        Dim arr(1 To 2) As Integer
        arr(1) = 4
        arr(2) = 8
        Call TestArray2(arr)
        Debug.Print arr(1) & "  " & arr(2)
    End Sub
    
    Sub TestArray2(arr() As Integer)
        Dim intI As Integer
        For intI = 1 To UBound(arr)
            arr(intI) = arr(intI) * 2
        Next intI
    End Sub
    You don't need to have the same name in both procedures/functions - I just used a short name.

    Hope this gives you a better understanding,

    Russell
    hi i'm trying to do something similar EXCEPT i don't declare the type of the array at the beginning. so i'm getting an error when i try passing the array. here's some code to further explain (and there is a reason i use dim then redim)
    Code:
    Sub TestArray1()
        Dim arr
        ReDim arr(1 To 2, 1 To 2)
        arr(1, 1) = 4
        arr(1, 2) = 5
        arr(2, 1) = 8
        arr(2, 2) = 6
        Call TestArray2(arr)
        Debug.Print arr(1, 1) & "  " & arr(1, 2) & "  " & arr(2, 1) & "  " & arr(2, 2)
    End Sub
    
    Sub TestArray2(arr() As Integer)
        Dim intI As Integer
        Dim x As Integer
        
        For intI = 1 To UBound(arr)
            For x = 1 To 2
            arr(intI, x) = arr(intI, x) * 2
            Next x
        Next intI
    End Sub
    thanks

  7. #7
    Board Regular
    Join Date
    May 2002
    Location
    mtl, canada
    Posts
    160
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    for those interested my mistake was quite dumb:
    it should read
    Dim arr() integer <-- notice the ()

    Code:
    Sub TestArray1()
        Dim arr()
        ReDim arr(1 To 2, 1 To 2)
        arr(1, 1) = 4
        arr(1, 2) = 5
        arr(2, 1) = 8
        arr(2, 2) = 6
        Call TestArray2(arr)
        Debug.Print arr(1, 1) & "  " & arr(1, 2) & "  " & arr(2, 1) & "  " & arr(2, 2)
    End Sub
    
    Sub TestArray2(arr() As Integer)
        Dim intI As Integer
        Dim x As Integer
        
        For intI = 1 To UBound(arr)
            For x = 1 To 2
            arr(intI, x) = arr(intI, x) * 2
            Next x
        Next intI
    End Sub

  8. #8
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,959
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Just one more thing to add

    ByRef is the default, so, if its ommitted, VBA will understand that the variable being passed is ByRef. If you're using ByVal, you need to write it that way.
    Regards,

    Juan Pablo González
    http://www.juanpg.com

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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