Solved

Return vba array from user defined function

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The ability to add structure to Visio diagrams using containers, lists and callouts is one of my favorite features in Visio 2010. In this article we’ll examine lists. We’ll explore containers and callouts in separate articles. Prior to reading th…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now