Reputation: 437
I have a sub (initialisation
) which contains several arrays. I am then creating another sub (testSub
) which have to access one of the arrays in initialisation
. I am unsure how to do this and would appreciate any help you can offer.
Initialisation sub:
Sub initialisation()
init_array = Array("apple", "orange", "car")
init_array_2 = Array("coconut", "keys", "blue")
End Sub
testSub:
Sub testSub()
For Each element in init_array 'Does not work currently
[do stuff]
Next
End Sub
Upvotes: 0
Views: 574
Reputation: 37337
You can also define your arrays outside subs:
Option Explicit
Dim init_array As Variant, init_array_2 As Variant
Sub initialisation()
init_array = Array("apple", "orange", "car")
init_array_2 = Array("coconut", "keys", "blue")
End Sub
Sub testSub()
Dim element As Variant
For Each element In init_array 'Does not work currently
[do stuff]
Next
End Sub
Upvotes: 2
Reputation: 5174
You need to pass it as parameter like this:
Option Explicit
Sub initialisation()
Dim init_array As Variant, init_array_2 As Variant
init_array = Array("apple", "orange", "car")
init_array_2 = Array("coconut", "keys", "blue")
testSub init_array
End Sub
Sub testSub(init_array As Variant)
Dim element As Variant
For Each element In init_array 'Does not work currently
[do stuff]
Next
End Sub
You should also use Option Explicit
which will force you to declare all your variables.
Upvotes: 4