Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Array Argument Parenthesis
An array is created in a sub, call it TestAy
it's passed to a call. What is the technical difference between A and B? call SubA(TestAy) ' no parens follow call SubB(TestAy()) ' parens follow Does it matter if TestAy is ReDim'd in the called sub the parens ? Thanks -- Neal Z |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Array Argument Parenthesis
Doesn't the second one error for you, expecting an index number?
It matters if it is re-dimmed, but that may be what you want. If it is passed ByRef and you redim it., the caller will see the changes. For instance Public Sub Test() Dim ary ary = Array(1, 2, 3) MsgBox ary(UBound(ary)) Call Called(ary) MsgBox ary(UBound(ary)) End Sub Sub Called(ByRef ary As Variant) ReDim Preserve ary(UBound(ary) + 1) ary(UBound(ary)) = 4 End Sub but if paseed ByVal, it won't. For instance Public Sub Test() Dim ary ary = Array(1, 2, 3) MsgBox ary(UBound(ary)) Call Called(ary) MsgBox ary(UBound(ary)) End Sub Sub Called(ByVal ary As Variant) ReDim Preserve ary(UBound(ary) + 1) ary(UBound(ary)) = 4 End Sub -- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "Neal Zimm" wrote in message ... An array is created in a sub, call it TestAy it's passed to a call. What is the technical difference between A and B? call SubA(TestAy) ' no parens follow call SubB(TestAy()) ' parens follow Does it matter if TestAy is ReDim'd in the called sub the parens ? Thanks -- Neal Z |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Array Argument Parenthesis
Thanks Bob, but I asked a crappy question, not homing in on what I wanted
to know, which is if you're processing the entire array in the called Sub, do the parenthesis matter, except perhaps for visual documentation in that you know that you're passing an array in the callER sub and not a variable( ignoring var naming conventions) BTW there's a new question something you mentioned in your answer at the end of this writeup. I'm relatively new to VBA, so, is there some deeper technical issue not in the MSo Help for: call Two(Ary) versus Call Two(Ary()) when the entire array is being processed? I'm nearing the end of a large effort where in the beginning I used call Two(Ary) with no apparent problems. After seeing Call Two(Ary()) used in this community and other sources I started using it, again, with no apparent problems. So, should I go back and re-visit the old code to be consistant ? I try whereever practical to develop my code modularly, via calls for easier testing, debugging, and use by more than 1 macro. My very typical and VERY abbreviated use is: Sub One() Dim Ary() as whatever ' lots more code...... Sub One may or may not ReDim Ary and fill it with data. call Sub Two(Ary) ' very very rarely do I use Ary(x) as an argument to pass 'only 1 value to Sub Two End Sub That's why I've never errored out in sub two 'expecting' an index. Sub Two typically processes the entire array and returns all the values to Sub One where it's processed further, and perhaps by other calls in Sub One. I don't remember the specifics, but I think I remember getting an error when I tried to use a byval array in an argument, so I was 'surprised' by your question. Can you pass an array byval as an argument?? I've not tested or tried this on purpose. My byval use was an unintended typo in the code. Sorry for being so long winded here. thanks for your help. -- Neal Z "Bob Phillips" wrote: Doesn't the second one error for you, expecting an index number? It matters if it is re-dimmed, but that may be what you want. If it is passed ByRef and you redim it., the caller will see the changes. For instance Public Sub Test() Dim ary ary = Array(1, 2, 3) MsgBox ary(UBound(ary)) Call Called(ary) MsgBox ary(UBound(ary)) End Sub Sub Called(ByRef ary As Variant) ReDim Preserve ary(UBound(ary) + 1) ary(UBound(ary)) = 4 End Sub but if paseed ByVal, it won't. For instance Public Sub Test() Dim ary ary = Array(1, 2, 3) MsgBox ary(UBound(ary)) Call Called(ary) MsgBox ary(UBound(ary)) End Sub Sub Called(ByVal ary As Variant) ReDim Preserve ary(UBound(ary) + 1) ary(UBound(ary)) = 4 End Sub -- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "Neal Zimm" wrote in message ... An array is created in a sub, call it TestAy it's passed to a call. What is the technical difference between A and B? call SubA(TestAy) ' no parens follow call SubB(TestAy()) ' parens follow Does it matter if TestAy is ReDim'd in the called sub the parens ? Thanks -- Neal Z |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
using array as argument in VBA sub | Excel Programming | |||
VBA function for "Mean" using Array as argument | Excel Worksheet Functions | |||
Function (array argument, range argument, string argument) vba | Excel Programming | |||
Need help passing an array as an argument | Excel Programming | |||
Array as Argument in User-Defined Functions | Excel Programming |