Results 1 to 4 of 4

Thread: Why doesn't my code work?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 1999
    Location
    Skudeneshavn, Norway
    Posts
    38
    Hi

    this is the code I hoped would remove
    blanck list items.

    VB doesn't like the line
    List1.RemoveItem x
    but I can't see anything wrong
    with it

    The code.


    Private Sub Command5_Click()

    For x = 0 To List1.ListCount

    If List1.List(x) = "" Then
    List1.RemoveItem x
    End If

    Next x
    List1.Refresh

    End Sub

    Thanks for your time

    Chris Davidsen
    Christian Davidsen

    If you go to sleep with an itchy
    ass, you wake up with smelly fingers.

  2. #2
    Guest

    Lightbulb A tip for Chris Davidson

    Hmm...simple problem, simple answer.
    Here's a solution:

    Code:
    Private Sub Command1_Click()
        Dim x As Long, c As Boolean
        Do Until x = List1.ListCount - 1
            c = False
            If List1.List(x) = "" Then
            List1.RemoveItem x
            c = True
            End If
            If c = False Then x = x + 1
        Loop
        List1.Refresh
    End Sub
    Your code:

    Code:
        For x = 0 To List1.ListCount - 1
    
        If List1.List(x) = "" Then
        List1.RemoveItem x
        'Whenever you remove an item, the LISTCOUNT changes, but VB would still
        'try to get x to the OLD Listcount, so using Do Until solves the problem!
        'You can also use Do While.
        End If
        
        Next x
        List1.Refresh
    Satisfactory?

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 1999
    Location
    Skudeneshavn, Norway
    Posts
    38

    Thanks man :-)

    Chris
    Christian Davidsen

    If you go to sleep with an itchy
    ass, you wake up with smelly fingers.

  4. #4
    Guest

    Thumbs up

    No problem. Glad to help.

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