extracting a subset of an array with VBA.

  • Thread starter Thread starter y
  • Start date Start date
Y

y

Hi all,

I need to extract an array from another. The dest array will contain the even element of the first one.

Which code of VBA could help me?
I've thought something this

Function Subset (source As range) As ? (I don't know, I need an array, not a range)
.... my algorithm ...
End Function

This function will be called in a cell as an argument inside several Excel functions.
Could someone tell me how can I return an array? Do I have to take care about some troubles in
particular in building this function?

Thanks in advance,
Alex.
 
Hi Alex,

Try this

Redim ary2(Int(Ubound(ary1,1)/2)
For i = 2 To Ubound(ary1,1) step 2
ary2(j) = ary1(i)
j=j+1
Next i

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Thanks Bob,

how do I have to specify the type of function?
I never didn't need to return an array.

Perhaps this way?

Function Subset(ary1 As range) As (10,10)
Redim ary2(Int(Ubound(ary1,1)/2)
For i = 2 To Ubound(ary1,1) step 2
ary2(j) = ary1(i)
j=j+1
Next i
Subset = Ary1
End Function

I apologize if my question is trivial.

Thanks again Alex.
 
Not quite, like this

Function Subset(ary1 As range)
Dim ary2
Redim ary2(Int(Ubound(ary1,1)/2)
For i = 2 To Ubound(ary1,1) step 2
ary2(j) = ary1(i)
j=j+1
Next i
Subset = ary2
End Function

You don't specify the size, as it is calculated in the function itself, and
you were returning the wrong array.
--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Back
Top