Solved

Return vba array from user defined function

Posted on 2011-09-20
5
843 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

688 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