I have an array. some of the elements in this array is empty. How can I kick away all those that is empty and the whole array can move upward? (i.e. pack all those are empty)
Printable View
I have an array. some of the elements in this array is empty. How can I kick away all those that is empty and the whole array can move upward? (i.e. pack all those are empty)
That's possible, I'm a bit busy right now, but I'll make a sample code for you as soon as I can, and when the solution isn't already given.
Quintonir
Ok, here is the code. It's a simple example of how to shorten it, but it'll teach you the basics.
----------------
Dim sArray(): ReDim sArray(1 To 10)
Dim Count, Count2 As Long
sArray(1) = "Hello"
sArray(2) = ""
sArray(3) = "My"
sArray(4) = "Name"
sArray(5) = "Is"
sArray(6) = "Quintonir"
sArray(7) = ""
sArray(8) = "What"
sArray(9) = "Is"
sArray(10) = "Yours?"
For Count = UBound(sArray) To LBound(sArray) Step -1
If sArray(Count) = "" Then
For Count2 = Count To UBound(sArray) - 1
sArray(Count2) = sArray(Count2 + 1)
Next
sArray(UBound(sArray)) = ""
ReDim Preserve sArray(1 To (UBound(sArray) - 1))
End If
Next
' Array fixed!
----------------
By the way, if you know how to insert a VB code (some users can automate something) please tell me :)
Quintonir
Try this:
Code:Dim sArray() As String
Dim A As Long
Dim Count As Long
ReDim sArray(9)
sArray(0) = "Hello"
sArray(1) = "My"
sArray(2) = ""
sArray(3) = "Name"
sArray(4) = "Is"
sArray(5) = "Quintonir"
sArray(6) = ""
sArray(7) = "What"
sArray(8) = "Is"
sArray(9) = "Yours?"
Count = 0
For A = 0 To UBound(sArray, 1)
If Not sArray(A) = "" Then
sArray(Count) = sArray(A)
Count = Count + 1
End If
Next
Count = Count - 1
If Count < 0 Then
Erase sArray
Else
ReDim Preserve sArray(Count)
End If
'Code improved by vBulletin Tool (Save as...)
Hiya Fox, could you please share the knowledge on how to add the code in Visual Basic format ??
See the last line in my code ;) I use my vB Tool to coloize the code..
But you may also use the [code] 'Code here [/code] -tags to get formatted code without colors..
Thank you very much, that will improve people's understanding of my posted codes!
True true... unformatted code is worse than anything else ;)
Ok, now that I have found out the new way of posting code:
Code:Dim sArray(): ReDim sArray(1 To 10)
Dim Count, Count2 As Long
sArray(1) = "Hello"
sArray(2) = ""
sArray(3) = "My"
sArray(4) = "Name"
sArray(5) = "Is"
sArray(6) = "Quintonir"
sArray(7) = ""
sArray(8) = "What"
sArray(9) = "Is"
sArray(10) = "Yours?"
For Count = UBound(sArray) To LBound(sArray) Step -1
If sArray(Count) = "" Then
For Count2 = Count To UBound(sArray) - 1
sArray(Count2) = sArray(Count2 + 1)
Next
sArray(UBound(sArray)) = ""
ReDim Preserve sArray(1 To (UBound(sArray) - 1))
End If
Next
' Array fixed!
Wasn't my solution better? :D