473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Returning an array from a function

TheServant
1,168 Expert 1GB
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?
Sep 15 '09 #1
3 1169
MikeTheBike
639 Expert 512MB
@TheServant
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
Sep 15 '09 #2
TheServant
1,168 Expert 1GB
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.
Sep 15 '09 #3
TheServant
1,168 Expert 1GB
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.
Sep 16 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.