Is it possible to pass an array as an argument variable...

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
If I call "sub_A" from within "sub_B", and "sub_A" fills an array, what's the best way to get that array back over to "sub_B" without making it public?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If I call "sub_A" from within "sub_B", and "sub_A" fills an array, what's the best way to get that array back over to "sub_B" without making it public?
Short answer is yes. Try this example:
Code:
Sub A()
Dim myArr(1 To 10)
For i = 1 To 10
myArr(i) = 1
Next i
Call B(myArr)
End Sub
Sub B(vArr() As Variant)
For j = LBound(vArr) To UBound(vArr)
vArr(j) = 2 * vArr(j)
Next j
ActiveSheet.Range("A1").Resize(1, UBound(vArr)).Value = vArr
End Sub
 
Upvote 0
Actually you know I think I'm going to need to use a function because I'm going to want to call multiple procedures in multiple class modules from a single source sub.

I've tried to get a function to equal an array that is set within the function. It looks like its working.

Code:
Public Static Sub test()
For Each element In test2
MsgBox test2(element)
Next
End Sub

Code:
Private Function test2()
Dim a()
For b = 1 To 2
ReDim Preserve a(1 To b)
a(b) = b
Next
test2 = a()
End Function

Thanks for the reply.
 
Upvote 0
Hmm. Wait, that's really inefficient, isn't it? I guess I want to assign the value of the function to another array in sub_A...? Otherwise it's running every singe time the loop takes place, plus one extra time as the loop is initialized.
 
Upvote 0
Is there a way to do something like:

Call Sub_B(array var to carry back from sub_B)

In sub_A? A statement that specifies that an argument is not passed to a sub, but rather is returned from it?

Everything I can think of with a function requires looping through the function like I did above in order to get all the elements of an array... I'd like to just grab the entire array, instead of havin to create it in one sub and then recreate it in another.
 
Upvote 0
Oh... Never mind, the obvious way is sometimes the right way, I guess.

Code:
Public Static Sub test()
Dim b()
b() = test2
For Each element In b()
MsgBox b(element)
Next
End Sub

Code:
Private Function test2()
Dim a()
For b = 1 To 2
ReDim Preserve a(1 To b)
a(b) = b
Next
test2 = a()
End Function

Thanks again.
 
Upvote 0
Maybe I'm not understanding what you want to do, but why not create the array in a UDF? In sub A dim a variant array say myArr(y to z) where y and z are variables that you can set in sub A. Then sub A calls UDF MakeArray(myArr) and returns myArr to sub A? You can pass complete arrays between the sub and UDF in both directions.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,672
Members
452,937
Latest member
Bhg1984

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