Vba syntax question: Passing variables into Function, returning array of longs

SkiHood

New Member
Joined
Oct 1, 2013
Messages
9
Something like...

dim retArray (0 to 1) as Long

reDim retArray ( 0 to 1)
retArray = doSomething(wSheet, searchVal, rngStart, rngEnd) (

Function doSomething(w, srch, begin, end) ()

retArray(0) = 100,000
retArray(1) = 5

doSomething = retArray

End function

The psuedo-code above is presented only to hopefully illustrate the question.

thanks in advance!

Ski
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You will need to pass your variables to the function. Use them as needed and then set your retArray to your function Array.

Code:
Sub arrayTest()
Dim retArray() As Long
retArray = doSomething(wSheet, searchhVal, rngStart, rngEnd)

End Sub

Function doSomething(w As Worksheet, srch As String, beginRange As Range, endRange As Range)
Dim newArray() As Long
'Here you would use your passed variable to do something to the array (newArray) in your function

'Then set retArray to newArray
retArray = newArray
End Function
 
Upvote 0
A bit vague (;)) but something like this:
Code:
Sub testing()
   Dim wSheet, searchVal, rngStart, rngEnd
   Dim retArray()                  As Long

   retArray = doSomething(wSheet, searchVal, rngStart, rngEnd)
   MsgBox retArray(0)
   MsgBox retArray(1)
End Sub
Function doSomething(w, srch, begin, lEnd) As Long()
   Dim tempArray(0 To 1)           As Long
   
   tempArray(0) = 100000
   tempArray(1) = 5

   doSomething = tempArray

End Function
 
Upvote 0
Rory's code is correct (and almost identical to mine) but note that he set the function to the tempArray (newArray in my example) which is correct.
 
Upvote 0
ah... found my issue. The variable assigned to receieve the result of the function has to be a variant, not an array!

Dim retArray As Variant

retArray = doSomething(wSheet, searchVal, rngStart, rngEnd)

Seems to have done the trick - at least I am seeing believable results while single stepping
 
Upvote 0
No - it can be an array (as in my code) but it must be dynamic and of the same type as the function.
 
Upvote 0
Ah... Silly me but how do I declare a dynamic array vs static ? I'm guessing but I suppose that my variant type is to be avoided do the the lack of typecasting ?
 
Upvote 0
Code:
dim retArray() as long
as opposed to
Code:
dim retArray(0 to 1) as long
 
Upvote 0

Forum statistics

Threads
1,213,529
Messages
6,114,155
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top