Solved

removing items from an array

Posted on 2004-09-03
3
214 Views
Last Modified: 2010-05-02
How do I do the following:

I have a dynamic array: ItemList() and it contains the following 7 items of data:

Bob
Anne
Jane
Mary
Tim
Jack
John

along the way, items are tagged as no longer availible and I've replaced them with [REMOVED] so my list of 7 items now reads:

Bob
Anne
[REMOVED]
Mary
Tim
[REMOVED]
John

whats the best way to take away the [REMOVED] items so i am left with an array of 5 items:

Bob
Anne
Mary
Tim
John

Cheers,

`Andy
0
Comment
Question by:xizor
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
JR2003 earned 125 total points
Comment Utility
This should do the trick:

Private Sub Command1_Click()
    Dim MyArray() As String
    ReDim MyArray(1 To 5)
    MyArray(1) = "qwe"
    MyArray(2) = "[REMOVED]"
    MyArray(3) = "qwe"
    MyArray(4) = "[REMOVED]"
    MyArray(5) = "qwe"
    Dim i As Long
    Dim j As Long
    j = 0 'Initialise
    For i = LBound(MyArray) To UBound(MyArray)
        If (i + j) > UBound(MyArray) Then Exit For
        If MyArray(i + j) = "[REMOVED]" Then
            j = j + 1
        End If
        If j <> 0 And (i + j) <= UBound(MyArray) Then
            MyArray(i) = MyArray(i + j)
        End If
    Next i
    If j <> 0 Then
        ReDim Preserve MyArray(LBound(MyArray) To UBound(MyArray) - j)
    End If
End Sub
0
 
LVL 2

Author Comment

by:xizor
Comment Utility
Thanks!
0
 
LVL 18

Expert Comment

by:JR2003
Comment Utility
Ooops, I just realised there is a bug in the code I just geve you.
This version works
JR

Private Sub Command1_Click()
    Dim MyArray() As String
    ReDim MyArray(1 To 6)
    MyArray(1) = "[REMOVED]"
    MyArray(2) = "[REMOVED]"
    MyArray(3) = "[REMOVED]"
    MyArray(4) = "QWE4"
    MyArray(5) = "[REMOVED]"
    MyArray(6) = "[REMOVED]"
    Dim i As Long
    Dim j As Long
    j = 0 'Initialise
    For i = LBound(MyArray) To UBound(MyArray)
        If i + j > UBound(MyArray) Then Exit For
        MyArray(i) = MyArray(i + j)
        While (MyArray(i) = "[REMOVED]") And (i + j) < UBound(MyArray)
            j = j + 1
            MyArray(i) = MyArray(i + j)
        Wend
    Next i
    If j <> 0 Then
        ReDim Preserve MyArray(LBound(MyArray) To UBound(MyArray) - j - LBound(MyArray))
    End If
    For i = LBound(MyArray) To UBound(MyArray)
        Debug.Print MyArray(i)
    Next i
End Sub

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now