Solved

Return vba array from user defined function

Posted on 2011-09-20
5
838 Views
Last Modified: 2013-11-27
I'm having difficulties with the basics of passing an array from a function in VBA.
To be used in vba macros in MS Office (Excel & Visio), but without reference to excel ranges.

I start by making a simple dynamic array:

Sub makeArray()
Dim arrItems() As Long
Dim i As Integer
Dim m As Long
m = 2
For i = 0 To 9
ReDim Preserve arrItems(i + 1)
arrItems(i) = i * 10 * m
Next i
For i = 0 To UBound(arrItems) - 1
Debug.Print "arrItems(" & i & ") = "; arrItems(i)
Next i
End Sub

How do I convert the above to a function to be called from this Sub():
Sub doMakeArray()
myArray() as long
Dim m as long
m = 2
myArray =doMakeArray(m)
end sub

I would start off:
Function doMakeArray(m as long)

I would seek to conclude:
doMakeArray = arrItems

I wish to return arrays of either strings or long - data types

Thanks
Kelvin4
 
0
Comment
Question by:Kelvin4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 24

Accepted Solution

by:
StephenJR earned 250 total points
ID: 36567548
Do you mean like this?
Sub x()

Dim myArray() As Long
Dim m As Long

m = 2
myArray = doMakeArray(m)

End Sub

Function doMakeArray(m As Long)

Dim arrItems() As Long
Dim i As Integer

ReDim arrItems(10)

For i = LBound(arrItems) To UBound(arrItems)
    arrItems(i) = i * 10 * m
Next i

doMakeArray = arrItems

End Function

Open in new window

0
 
LVL 24

Expert Comment

by:StephenJR
ID: 36567554
Actually line 16 should be ReDim arrItems(9)
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 36567851
You can use:

Function doMakeArray(m As Long) As Variant

That will allow you to return an array.

/gustav
0
 
LVL 11

Assisted Solution

by:Visio_Guy
Visio_Guy earned 250 total points
ID: 36568039
More on this. If you know the size of the array in advance, you can specify it at declaration. If the size is stored in a variable, then you have to use ReDim, but it's probably best to keep ReDim out of loops.

Sub ArrayStuff()

  '// Two ways to pre-size an array in VBA:
  Dim longArray(10) As Long
  Dim stringArray(0 To 14) As String
  
  '// Check out their bounds:
  Debug.Print LBound(longArray) '.....0
  Debug.Print UBound(longArray) '.....10
  
  Debug.Print LBound(stringArray) '...0
  Debug.Print UBound(stringArray) '...14
  
  
  '// Use a variable to specify array size:
  Dim m As Integer
  m = 12
  Dim intArray() As Integer
  ReDim intArray(1 To m - 1)
  
  Debug.Print LBound(intArray) '...0
  Debug.Print UBound(intArray) '...11
  
End Sub

Open in new window

0
 

Author Closing Comment

by:Kelvin4
ID: 36570357
Thanks!
Kelvin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question