Suppose you have a set of arrays that contain one extra element at the
end that you do not need. You want to remove it before sending the
array on into another function that will do calculations on its
values.
Okay, what I have done is this:
Redim Preserve MyArray(UBound(MyArray)-1)
This works fine for removing the last array element, EXCEPT when there
is only one element, MyArray(0). Here you get an error.
So what I have done is this:
If UBound(MyArray) > 0 then Redim Preserve MyArray(UBound(MyArray)-1)
It's a lot of verbage though, so I'm thinking that maybe there is a
better way to do this?
Thanks.
Webbiz
Nobody - 29 Jul 2009 01:31 GMT
> Suppose you have a set of arrays that contain one extra element at the
> end that you do not need. You want to remove it before sending the
[quoted text clipped - 14 lines]
> It's a lot of verbage though, so I'm thinking that maybe there is a
> better way to do this?
Yes:
Sub RemoveLastElement(ByRef a() As Single)
If UBound(a) > 0 Then
Redim Preserve a(UBound(a)-1)
End If
End Sub
Larry Serflaten - 29 Jul 2009 02:14 GMT
> It's a lot of verbage though, so I'm thinking that maybe there is a
> better way to do this?
It is a dynamic array. Somewhere in your code you've created it to include
one extra element. Why is that?
When you create it the first time, omit that extra element, problem solved....
<g>
LFS