Solved

How do I delete all "empty" elements of my array

Posted on 2010-08-28
2
927 Views
Last Modified: 2012-05-10
Hi there,

I need to delete all "empty" elements of my array.
How do I do this?

Thanks,
0
Comment
Question by:Quickstepbr
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33551217
Try this
Function DeleteEmpty(ByRef Arr As Variant) As Variant
If Not IsArray(Arr) Then Exit Function

Dim i As Long
Dim acount As Long
Dim arrCopy()
ReDim Preserve arrCopy(UBound(Arr))

acount = 0
For i = LBound(Arr) To UBound(Arr)
If Arr(i) & "" > "" Then
    arrCopy(acount) = Arr(i)
    acount = acount + 1
End If
Next

ReDim Preserve arrCopy(acount - 1)
DeleteEmpty = arrCopy
End Function

Sub x()
Dim ars(5) As String
ars(1) = "test"
ars(2) = ""
ars(3) = ""
ars(4) = "blah"
ars(5) = "blah"
MsgBox Join(DeleteEmpty(ars), ",")

End Sub

Open in new window

0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 33551233
An alternative:

    Y = "|" & Join(X, "|") & "|"
    Do: Y = Replace(Y, "||", "|"): Loop While Len(Replace(Y, "||", "|")) <> Len(Y)
    X = Split(Mid(Y, 2, Len(Y) - 2), "|")

X is the array (a Variant) and Y is a string.

Kevin
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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