Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-28
2
Medium Priority
?
954 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

916 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