Results 1 to 23 of 23

Thread: Is there a way to search a list for an item, then remove it?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Is there a way to search a list for an item, then remove it?

    Hi there everyone. I am working on a math game where students can make their own custom game patterns. I am working on a way for students to be able to delete custom patterns down the road.

    So far I have this..
    VB Code:
    1. Dim iczg As Integer
    2.    Dim srch_stringzg As String
    3.    srch_stringzg = LoadGameNameTXT.text    'or whatever you want to search for
    4.    For iczg = 0 To CustomGameList.ListCount - 1
    5.       If CustomGameList.List(iczg) = srch_stringzg Then
    6.          CustomGameList.RemoveItem srch_stringzg
    7.  
    8.       End If
    9.    Next iczg

    Basically the name of the pattern is in the LoadGameNameTXT.text. I am trying to get the code to search for it in the CustomGameList listbox, and if found remove it from the listbox, but I am getting a type mismatch error on this line..

    CustomGameList.RemoveItem srch_stringzg

    Can anyone tell me what I did wrong?

    Thanks!!

  2. #2
    Hyperactive Member Rattled_Cage's Avatar
    Join Date
    Dec 2005
    Posts
    315

    Re: Is there a way to search a list for an item, then remove it?

    remove the item by index and not the string it looks like

    change yours to
    Code:
    CustomGameList.RemoveItem iczg

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Is there a way to search a list for an item, then remove it?

    Also process the List backwards, otherwise the indexing goes wrong!

  4. #4
    Lively Member
    Join Date
    Oct 2013
    Posts
    124

    Re: Is there a way to search a list for an item, then remove it?

    Quote Originally Posted by Arnoutdv View Post
    Also process the List backwards, otherwise the indexing goes wrong!
    Honest question. No harm intended.

    This is another area I don't have much experience in.

    Does the conflict arise because a member of the list has been deleted, but the list hasn't been re-indexed and the program thinks that member is still there?

  5. #5
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Is there a way to search a list for an item, then remove it?

    No problem Honduras!

    The problem arises when you have 2 items in a row which need to be removed.
    If you need to remove item 5 + 6, then after you remove item 5, item 6 will become the new item 5.
    But the For Next loop sets your Counter to 6, so you skip the new item 5
    Code:
    Private Sub Command2_Click()
      Dim cCol As Collection
      Dim i As Long
      
      Set cCol = New Collection
      
      cCol.Add "AAA"
      cCol.Add "BBB"
      cCol.Add "BBB"
      cCol.Add "CCC"
    
      Debug.Print "---"
      Debug.Print "Forwards"
      Debug.Print "---"
    
      ' We want to remove all items with "BBB"
      For i = 1 To cCol.Count - 1
        If cCol(i) = "BBB" Then cCol.Remove i
      Next i
      
      For i = 1 To cCol.Count
        Debug.Print cCol(i)
      Next i
      
      Debug.Print "---"
      Debug.Print "Backwards"
      Debug.Print "---"
      
      Set cCol = New Collection
      
      cCol.Add "AAA"
      cCol.Add "BBB"
      cCol.Add "BBB"
      cCol.Add "CCC"
        
      ' We want to remove all items with "BBB"
      For i = cCol.Count - 1 To 1 Step -1
        If cCol(i) = "BBB" Then cCol.Remove i
      Next i
      
      For i = 1 To cCol.Count
        Debug.Print cCol(i)
      Next i
      
    End Sub

  6. #6
    Lively Member
    Join Date
    Oct 2013
    Posts
    124

    Re: Is there a way to search a list for an item, then remove it?

    So you have to take an action after ever deletion. Re-index or re-create the array? That's good to know and may explain some problems I have had in the past.

    It's good to keep in mind that a program does things, and usually well. But there are some it doesn't do. Knowing the difference is what makes us all millionaires.

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Is there a way to search a list for an item, then remove it?

    The action is, loop backwards.
    Just test my example, it does both and shows the results.

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Is there a way to search a list for an item, then remove it?

    Yep backwards is the ticket, this prevents the unchecked entries from changing their index numbers

    So if you loop forwards and have 10 items in the list when you remove item 2 then item 3 becomes item 2 and you move on to item 3 which used to be item 4 so 3 is skipped over and not checked and then of course you will get an index out of bounds when you reach item 10 as it no longer exists.

    Looping backwards the indexes change only on those items that have already been tested so you do not run into these issues

    btw wasn't this same question asked by the Op and answered just a few days ago?

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Is there a way to search a list for an item, then remove it?

    Thank you guys, I just have a small follow up question.

    Is there a way to search for blank list items? Like nothing is there, and if there is a blank item, remove it?



    I tried using the above code, but it gives me a type mismatch error when trying to find "nothing" lol.

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Is there a way to search a list for an item, then remove it?

    If List1.List(n) = "" Then List1.RemoveItem n

    or if you are really checking for blank spaces (not null as above)

    Dim Lgth As Integer

    Lgth = Len(List1(n))

    If List1.List(n) = Space(Lgth) Then.......
    Last edited by jmsrickland; Nov 29th, 2013 at 08:57 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Is there a way to search a list for an item, then remove it?

    Awesome thanks! Would n be dimmed as a string in this case?

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Is there a way to search a list for an item, then remove it?

    No, n is an Integer which I used as the index I think you are using iczg


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  13. #13
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Is there a way to search a list for an item, then remove it?

    One go
    Code:
    If Trim$(List1.List(n)) = "" Then List1.RemoveItem n



  14. #14
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Is there a way to search a list for an item, then remove it?

    Good show, 4x2y


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  15. #15

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Is there a way to search a list for an item, then remove it?

    Thank you for the help.

    Just as a follow up question. Lets say I have a textbox instead of listbox

    Would

    If Trim$(List1.List(n)) = "" Then List1.RemoveItem n

    become

    Dim
    If Trim$(Text1.text(n)) = "" Then
    Text1.text = Text1.text - n
    end if

    ???

  16. #16
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,906

    Re: Is there a way to search a list for an item, then remove it?

    The Texbox.Text holds a single string. Not a string array.

  17. #17

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Is there a way to search a list for an item, then remove it?

    Ok, to make things more complicated lets say the stuff I needed to remove was always in brackets (), and they are always infront/on the left side of the text I want to keep, is there a way to get rid of the text in the () including the brackets from the textbox?

  18. #18
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Is there a way to search a list for an item, then remove it?

    Code:
    s = "(123)45678"
    s = Mid$(s, InStr(1, s, ")") + 1)
    MsgBox s



  19. #19

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Is there a way to search a list for an item, then remove it?

    Thanks, would "s" in this case be a string or the textbox with the original text?

  20. #20
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Is there a way to search a list for an item, then remove it?

    Quote Originally Posted by Justin M View Post
    Thanks, would "s" in this case be a string or the textbox with the original text?
    Yes of course



  21. #21

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Is there a way to search a list for an item, then remove it?

    Oh this is awesome!

    So now my code looks like this.

    Dim s As String
    s = LoadGameNameTXT.text
    s = Mid$(s, InStr(1, s, ")") + 1)
    LoadGameNameTXT.text = s

    Would there be a way for it to check for cases where there could be two sets of brackets? like ()()

  22. #22

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Is there a way to search a list for an item, then remove it?

    For example, if I run the code twice would it get rid of the first set of brackets (), then if it runs again would it get rid the second set?

  23. #23
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Is there a way to search a list for an item, then remove it?

    If you are sure the part of the string you want to keep doesn't contain ")" then you can use InStrRev to get rid of all brackets in one step.
    Code:
        LoadGameNameTXT.Text = Mid$(LoadGameNameTXT.Text, InStrRev(LoadGameNameTXT.Text, ")") + 1)



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width