Connecting Tech Pros Worldwide Help | Site Map

Returning an array from a function

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#1: Sep 15 '09
Hi everyone,
I have been trying and searching for everything that I can think of, but I still have no solution, which makes me wonder if I'm on the wrong track.
Expand|Select|Wrap|Line Numbers
  1. Function tester() As Variant
  2.     Dim fray(11) As Variant
  3.  
  4.     For i = 1 To 12
  5.         fray(i - 1) = i
  6.     Next
  7.  
  8.     tester = fray
  9. End Function
Now my output is all 1's in each of the 12 cells that I have included in my excel array formula:
Expand|Select|Wrap|Line Numbers
  1. {=tester()}
This is probably because tester is not an array. My question is, how to I make tester and array, and are there any restrictions of function inputs because of this? Or am I totally on the wrong track?
Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 346
#2: Sep 15 '09

re: Returning an array from a function


Quote:

Originally Posted by TheServant View Post

Hi everyone,
I have been trying and searching for everything that I can think of, but I still have no solution, which makes me wonder if I'm on the wrong track.

Expand|Select|Wrap|Line Numbers
  1. Function tester() As Variant
  2.     Dim fray(11) As Variant
  3.  
  4.     For i = 1 To 12
  5.         fray(i - 1) = i
  6.     Next
  7.  
  8.     tester = fray
  9. End Function
Now my output is all 1's in each of the 12 cells that I have included in my excel array formula:
Expand|Select|Wrap|Line Numbers
  1. {=tester()}
This is probably because tester is not an array. My question is, how to I make tester and array, and are there any restrictions of function inputs because of this? Or am I totally on the wrong track?

Hi

before we can comment it would be useful to know precisely what you are trying to do with the array after you have assigned it to the function !!

For instance this produces the correct ouput
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Function tester() As Variant
  4.     Dim fray(11) As Variant
  5.     Dim i As Integer
  6.  
  7.     For i = 1 To 12
  8.         fray(i - 1) = i
  9.     Next i
  10.  
  11.     tester = fray
  12. End Function
  13.  
  14. Sub testFunction()
  15.     Dim ThisArray() As Variant
  16.     Dim i As Integer
  17.  
  18.     ThisArray = tester
  19.     For i = 0 To UBound(ThisArray)
  20.         MsgBox ThisArray(i)
  21.     Next i
  22.  
  23. End Sub
??

MTB
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#3: Sep 16 '09

re: Returning an array from a function


Thanks for your reply.
What I want is to cycle through several tables with the month key. For each row I want to do a complex function, which I could do in excel, but the if and nested if statements would just be a lot easier in VB. Now that would be simple to have a sub and go through that way...
However, if rows are inserted or deleted I would like it to still work, and since a sub would require defined ranges, I thought it would be simpler to have an excel input (which can be updated by excel as needed) and return the values.

Thanks for your time.
TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 913
#4: Sep 16 '09

re: Returning an array from a function


Solved:
Expand|Select|Wrap|Line Numbers
  1. Function tester() As Variant
  2.     Dim fray() As Variant
  3.     Dim i As Long
  4.  
  5.     ReDim fray(1 To 12, 1 To 1)
  6.     For i = 1 To 12
  7.         fray(i, 1) = i
  8.     Next
  9.     tester = fray
  10. End Function
Thanks for your help.
Reply