Returning array in function

Returning array in function

Anonymous
Not applicable
1,480 Views
10 Replies
Message 1 of 11

Returning array in function

Anonymous
Not applicable
is it possible to return an array from a Function in VBA. I tried a couple
different ways with no success.

example

MyArray() = MyFunction(Variable)

Thanks,
Edward
0 Likes
1,481 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
Try This

Private Sub TestArray()

Dim retArray() As String

retArray = ReturnArray

Debug.Print retArray(0); retArray(1)

End Sub


Private Function ReturnArray() As String()

Dim tmpArray(1) As String

tmpArray(0) = "Hello "
tmpArray(1) = "World"

ReturnArray = tmpArray

End Function

--
Veign
www.veign.com
Code Samples & Sample Projects
http://www.veign.com/information/application/info_app.html
Submit Your Best Code (you keep the rights)
http://www.veign.com/information/application/code_submit.html
---------
"Edward Bagby" wrote in message
news:[email protected]...
> is it possible to return an array from a Function in VBA. I tried a
couple
> different ways with no success.
>
> example
>
> MyArray() = MyFunction(Variable)
>
> Thanks,
> Edward
>
>
0 Likes
Message 3 of 11

Anonymous
Not applicable
You can't assign directly to an array.
If you dim MyArray as Variant you can then set it equal to the array
returned by MyFunction
Then calling MyArray(0) etc works just like it was an array( although it's a
variant)
Function testarray()
Dim ar(0 To 2) As Double
ar(0) = 1
ar(1) = 1
ar(2) = 1
testarray = ar
End Function

Sub testfunc()
Dim artest As Variant
artest = testarray
Debug.Print artest(0)
Debug.Print artest(1)
Debug.Print artest(2)
End Sub

"Edward Bagby" wrote in message
news:[email protected]...
> is it possible to return an array from a Function in VBA. I tried a
couple
> different ways with no success.
>
> example
>
> MyArray() = MyFunction(Variable)
>
> Thanks,
> Edward
>
>
0 Likes
Message 4 of 11

Anonymous
Not applicable
Is this a limitation of VBA? - I forgot that VBA may not currently allow the
return of Arrays from a Function..

--
Veign
www.veign.com
Code Samples & Sample Projects
http://www.veign.com/information/application/info_app.html
Submit Your Best Code (you keep the rights)
http://www.veign.com/information/application/code_submit.html
---------
"Mark Propst" wrote in message
news:[email protected]...
> You can't assign directly to an array.
> If you dim MyArray as Variant you can then set it equal to the array
> returned by MyFunction
> Then calling MyArray(0) etc works just like it was an array( although it's
a
> variant)
> Function testarray()
> Dim ar(0 To 2) As Double
> ar(0) = 1
> ar(1) = 1
> ar(2) = 1
> testarray = ar
> End Function
>
> Sub testfunc()
> Dim artest As Variant
> artest = testarray
> Debug.Print artest(0)
> Debug.Print artest(1)
> Debug.Print artest(2)
> End Sub
>
> "Edward Bagby" wrote in message
> news:[email protected]...
> > is it possible to return an array from a Function in VBA. I tried a
> couple
> > different ways with no success.
> >
> > example
> >
> > MyArray() = MyFunction(Variable)
> >
> > Thanks,
> > Edward
> >
> >
>
>
0 Likes
Message 5 of 11

Anonymous
Not applicable
Veign had this to say:

> Is this a limitation of VBA? - I forgot that VBA may not currently
> allow the return of Arrays from a Function..

No. VB6 and below won't allow you to return an array either. You must
instead declare the return type as Variant:

Public Function DoSomething() As Double() ' not allowed
Public Function DoSomething() As Variant ' allowed

--
http://www.acadx.com
"If you want to be somebody else change your mind"
0 Likes
Message 6 of 11

Anonymous
Not applicable
I wasn't aware that it was a vba issue, i've just always heard that you
could not "assign" directly to an array. (not "return an array from a
function")
so as in the example, the function returns an array but it must be assigned
to a variant.
I thought the only way to fill an array was
ar(0) = x
ar(1) = y
etc.
Is that not the case with vb?

"Veign" wrote in message
news:[email protected]...
> Is this a limitation of VBA? - I forgot that VBA may not currently allow
the
> return of Arrays from a Function..
>
> --
> Veign
> www.veign.com
> Code Samples & Sample Projects
> http://www.veign.com/information/application/info_app.html
> Submit Your Best Code (you keep the rights)
> http://www.veign.com/information/application/code_submit.html
> ---------
> "Mark Propst" wrote in message
> news:[email protected]...
> > You can't assign directly to an array.
> > If you dim MyArray as Variant you can then set it equal to the array
> > returned by MyFunction
> > Then calling MyArray(0) etc works just like it was an array( although
it's
> a
> > variant)
> > Function testarray()
> > Dim ar(0 To 2) As Double
> > ar(0) = 1
> > ar(1) = 1
> > ar(2) = 1
> > testarray = ar
> > End Function
> >
> > Sub testfunc()
> > Dim artest As Variant
> > artest = testarray
> > Debug.Print artest(0)
> > Debug.Print artest(1)
> > Debug.Print artest(2)
> > End Sub
> >
> > "Edward Bagby" wrote in message
> > news:[email protected]...
> > > is it possible to return an array from a Function in VBA. I tried a
> > couple
> > > different ways with no success.
> > >
> > > example
> > >
> > > MyArray() = MyFunction(Variable)
> > >
> > > Thanks,
> > > Edward
> > >
> > >
> >
> >
>
>
0 Likes
Message 7 of 11

Anonymous
Not applicable
So how come your example works if it's not supposed to be possible? It seems
you are returning an array and assigning to an array variable and it works!
why?

"Veign" wrote in message
news:[email protected]...
> Is this a limitation of VBA? - I forgot that VBA may not currently allow
the
> return of Arrays from a Function..
>
> --
> Veign
> www.veign.com
> Code Samples & Sample Projects
> http://www.veign.com/information/application/info_app.html
> Submit Your Best Code (you keep the rights)
> http://www.veign.com/information/application/code_submit.html
> ---------
> "Mark Propst" wrote in message
> news:[email protected]...
> > You can't assign directly to an array.
> > If you dim MyArray as Variant you can then set it equal to the array
> > returned by MyFunction
> > Then calling MyArray(0) etc works just like it was an array( although
it's
> a
> > variant)
> > Function testarray()
> > Dim ar(0 To 2) As Double
> > ar(0) = 1
> > ar(1) = 1
> > ar(2) = 1
> > testarray = ar
> > End Function
> >
> > Sub testfunc()
> > Dim artest As Variant
> > artest = testarray
> > Debug.Print artest(0)
> > Debug.Print artest(1)
> > Debug.Print artest(2)
> > End Sub
> >
> > "Edward Bagby" wrote in message
> > news:[email protected]...
> > > is it possible to return an array from a Function in VBA. I tried a
> > couple
> > > different ways with no success.
> > >
> > > example
> > >
> > > MyArray() = MyFunction(Variable)
> > >
> > > Thanks,
> > > Edward
> > >
> > >
> >
> >
>
>
0 Likes
Message 8 of 11

Anonymous
Not applicable
Mark Propst had this to say:

> So how come your example works if it's not supposed to be possible?
> It seems you are returning an array and assigning to an array
> variable and it works! why?

That much is indeed possible. The only hiccup is the return type
cannot be declared as an array since they are not OLE compliant. By
returning the array as a Variant, VB fills a Variant with a
self-describing safearray.

The function caller can assign the return value directly to an array
because the Variant contains an array.

--
http://www.acadx.com
"If you want to be somebody else change your mind"
0 Likes
Message 9 of 11

Anonymous
Not applicable
"Frank Oquendo" wrote in message
news:[email protected]...
> Mark Propst had this to say:
>
> > So how come your example works if it's not supposed to be possible?
> > It seems you are returning an array and assigning to an array
> > variable and it works! why?
>
> That much is indeed possible. The only hiccup is the return type
> cannot be declared as an array since they are not OLE compliant.

Private Function ReturnArray() As String()
That isn't declaring as an array?

>By
> returning the array as a Variant, VB fills a Variant with a
> self-describing safearray.
>
> The function caller can assign the return value directly to an array
> because the Variant contains an array.
>
> --
> http://www.acadx.com
> "If you want to be somebody else change your mind"
>
>
>
>
>
>
0 Likes
Message 10 of 11

Anonymous
Not applicable
Edward,

Another option is to declare the array in the calling procedure and include
it as a parameter of the function. This allows you to define the function as
a boolean

Function MyFunction (ByRef MyArray() as Double) as Boolean
' get the data to populate the array. I've used a Long "cnt" to hold
the number of items.
If cnt > 0 then
Redim MyArray(0 To cnt -1)
For I = 0 to cnt - 1
MyArray(I) = 1
Next
MyFunction= True
Else
MyFunction = False
End if
End Function

Then when you call the function

Dim MyArray() as Double
If MyFunction(MyArray) = True Then
'use the array
Else
'display error message
End if

Glen




"Edward Bagby" wrote in message
news:[email protected]...
> is it possible to return an array from a Function in VBA. I tried a
couple
> different ways with no success.
>
> example
>
> MyArray() = MyFunction(Variable)
>
> Thanks,
> Edward
>
>
0 Likes
Message 11 of 11

Anonymous
Not applicable
From the MSDN Library (VB6)

What's New in Language Features
Functions Can Return Arrays

All Editions Functions and properties procedures will be able to return
arrays.


--
Veign
www.veign.com
Code Samples & Sample Projects
http://www.veign.com/information/application/info_app.html
Submit Your Best Code (you keep the rights)
http://www.veign.com/information/application/code_submit.html
---------
"Frank Oquendo" wrote in message
news:[email protected]...
> Veign had this to say:
>
> > Is this a limitation of VBA? - I forgot that VBA may not currently
> > allow the return of Arrays from a Function..
>
> No. VB6 and below won't allow you to return an array either. You must
> instead declare the return type as Variant:
>
> Public Function DoSomething() As Double() ' not allowed
> Public Function DoSomething() As Variant ' allowed
>
> --
> http://www.acadx.com
> "If you want to be somebody else change your mind"
>
>
>
>
>
>
0 Likes