Passing an array between modules

DominikWells

New Member
Joined
Feb 21, 2002
Messages
7
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.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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
 
Upvote 0
It did help, thank you! I couldn't get the former portion to work, but the latter worked just fine. Thanks again.

DEW
 
Upvote 0
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.


_________________<font color = green> Mark O'Brien
This message was edited by Mark O'Brien on 2002-02-22 14:44
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,186
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top