Click to See Complete Forum and Search --> : Clearing Array Elements
ChrisJackson
Jan 17th, 2000, 05:34 AM
Does anyone know of a simple, easy way to delete empty array elements?
For example:
champ(0) = "one"
champ(1) = "two"
champ(2) = ""
champ(3) = "eight"
champ(4) = ""
champ(5) = "twenty"
How can I get rid of champ(2) and champ(4) while leaving the rest of the data intact?
Thanks,
Chris
MartinLiss
Jan 17th, 2000, 10:31 PM
Enjoy!
Dim champ() As String
Dim intEntry As Integer
Dim intEntry2 As Integer
Dim intBlankEntry As Integer
Dim bNoChanges As Boolean
Dim intNonBlank As Integer
Dim intHighEntry As Integer
ReDim champ(5)
champ(0) = "one"
champ(1) = "two"
champ(2) = ""
champ(3) = "eight"
champ(4) = ""
champ(5) = "twenty"
Do Until bNoChanges
For intEntry = 0 To UBound(champ)
bNoChanges = True
' Look fo a blank entry
If champ(intEntry) = "" Then
intBlankEntry = intEntry ' Store the blank entry's location
' Look for the next non-blank entry, starting at the entry that
' follows the blank one
For intEntry2 = intEntry + 1 To UBound(champ)
If champ(intEntry2) <> "" Then
' We found one
champ(intBlankEntry) = champ(intEntry2) ' Copy the non-blank value to the blank entry
champ(intEntry2) = "" ' Erase value we just copied
intHighEntry = intBlankEntry ' Store the "high water" mark
bNoChanges = False ' Record that we made a change
Exit For
End If
Next intEntry2
End If
Next intEntry
Loop
' Do this only if you want to compress your array.
' If you don't want to do this then you don't need
' to record intHighEntry above
ReDim Preserve champ(intHighEntry)
End Sub
------------------
Marty
[This message has been edited by MartinLiss (edited 01-18-2000).]
ChrisJackson
Jan 17th, 2000, 10:51 PM
Thanks for the code Marty. But I don't think that I'll Enjoy doing this. :)
Chris
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.