Home All Groups Group Topic Archive Search About

Remove empty elements from end of array

Author
20 Dec 2008 11:19 PM
Webbiz
What is the best, clean way to remove empty elements from the end of a VB
array?

After I run a FilterDuplicates function that gets rid of duplicate elements
and shifts the remaining elements to the lower element values, I'm left with
an array that has empty elements at the end.

Thank you in advance.

Webbiz

Author
20 Dec 2008 11:24 PM
Ivar
If you know how many elements are empty at the end, or how many elements at
the top of the array are needed then simply use Redim Preserve.

Redim Preserve ArrayName(ElementCount)

Look it up in MSDN

Ivar
Author
21 Dec 2008 12:13 AM
Webbiz
Of course! How silly of me.

Thanks!

Webbiz


Show quoteHide quote
"Ivar" <Ivar.ekstromer***@ntlworld.com> wrote in message
news:89f3l.34062$I05.18939@newsfe04.ams2...
> If you know how many elements are empty at the end, or how many elements
> at the top of the array are needed then simply use Redim Preserve.
>
> Redim Preserve ArrayName(ElementCount)
>
> Look it up in MSDN
>
> Ivar
>
Author
20 Dec 2008 11:25 PM
DanS
"Webbiz" <nospam@formethanks.com> wrote in
news:X3f3l.68579$5P1.14414@newsfe13.iad:

> What is the best, clean way to remove empty elements from the end of a
> VB array?
>
> After I run a FilterDuplicates function that gets rid of duplicate
> elements and shifts the remaining elements to the lower element
> values, I'm left with an array that has empty elements at the end.
>
> Thank you in advance.
>
> Webbiz

If it's a dynamic array, just.....

Redim Preserve DynArray(x)

.....with x being your last element.
Author
20 Dec 2008 11:26 PM
expvb
"Webbiz" <nospam@formethanks.com> wrote in message
news:X3f3l.68579$5P1.14414@newsfe13.iad...
> What is the best, clean way to remove empty elements from the end of a VB
> array?
>
> After I run a FilterDuplicates function that gets rid of duplicate
> elements and shifts the remaining elements to the lower element values,
> I'm left with an array that has empty elements at the end.

Use ReDim Preserve.
Author
21 Dec 2008 3:37 AM
Rick Rothstein
While you have your answer, I thought some might find this interesting. IF
the array MyArray is a String array, then you can remove the empty cells
without knowing the filled number of elements...

  Joined = Join(MyArray, Chr(1))
  Joined = Left(Joined, InStr(Joined, Chr(1) & Chr(1)) - 1)
  MyArray = Split(Joined, Chr(1))

where Joined is a String variable. And, while not efficient by any means,
the above can be squished together into a one-liner...

  MyArray = Split(Left(Join(MyArray, Chr(1)), InStr(Join(MyArray, _
                          Chr(1)), Chr(1) & Chr(1)) - 1), Chr(1))

You all just **knew** I had to do that, right? <g>
--
Rick (MVP - Excel)


Show quoteHide quote
"Webbiz" <nospam@formethanks.com> wrote in message
news:X3f3l.68579$5P1.14414@newsfe13.iad...
> What is the best, clean way to remove empty elements from the end of a VB
> array?
>
> After I run a FilterDuplicates function that gets rid of duplicate
> elements and shifts the remaining elements to the lower element values,
> I'm left with an array that has empty elements at the end.
>
> Thank you in advance.
>
> Webbiz
>
>
>
>