![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Feb 2002
Posts: 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. |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: California
Posts: 3,857
|
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 |
|
New Member
Join Date: Feb 2002
Posts: 7
|
It did help, thank you! I couldn't get the former portion to work, but the latter worked just fine. Thanks again.
DEW |
|
|
|
|
|
#4 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
|
Quote:
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 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
|
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
Hope this gives you a better understanding, Russell |
|
|
|
|
|
#6 | |
|
Board Regular
Join Date: May 2002
Location: mtl, canada
Posts: 160
|
Quote:
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
|
|
|
|
|
|
|
#7 |
|
Board Regular
Join Date: May 2002
Location: mtl, canada
Posts: 160
|
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 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
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. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|