does anyone know how to remove empty items in a listbox? here's an example of what is in my listbox...
#1
#2
(EMPTY SPACE)
#3
#4
(EMPTY SPACE)
#5
i want to remove those empty space, can someone help?
Printable View
does anyone know how to remove empty items in a listbox? here's an example of what is in my listbox...
#1
#2
(EMPTY SPACE)
#3
#4
(EMPTY SPACE)
#5
i want to remove those empty space, can someone help?
Hmm..
Probably would have to loop it and check per item index and check:
If Not Len(Trim(List1.List(Index))) Then ... 'fast compare, no string compare(string comp = slower)
Then you use RemoveItem(Index).
thanks, works perfectly. i just didnt know how to check if an item was empty
nevermind, i thought i had it working, but i didnt. here's what i have so far.
i got a run time error 5Code:Dim strCompare As String
Dim intCompare As Integer
For i = 0 To List1.ListCount - 1
strCompare = List1.List(i)
intCompare = Len(Trim(strCompare))
If intCompare = 0 Then 'if zero, it will remove it
list1.removeitem(i)
End If
Next i
On what line?
VB Code:
For i = 0 To List1.ListCount -1 If list1(i) = "" Or List1(i) = " " Then List1.RemoveItem i Next i
I think that works...
it highlights this line: List1.RemoveItem (i)
and Spajeoly, thanks for the help, but i get the same error
i think it's because when you remove an item from a listbox, the number of item changes. "for i = 0 to list1.listcount -1" counts the listbox at the beginning, but when you remove an item, the number of items changes. but i still dont know how to fix it...
change
VB Code:
If list1(i) = "" Or List1(i) = " " Then List1.RemoveItem i
to
[Highlight=VB]
If trim(list1(i)) = "" Then List1.RemoveItem i
[Highlight=VB]
That prevents any number of spaces being entered
VB Code:
Do until I > list1.listcount -1 'If deleted dont add 1 ' If not deleted add 1 to I Loop
the easiest way to solve it is to just go the other way through the list (start at the end and work towards the start)
VB Code:
Dim strCompare As String Dim intCompare As Integer For i = [b]List1.ListCount - 1 To 0 [i]Step -1[/i][/b] strCompare = List1.List(i) intCompare = Len(Trim(strCompare)) If intCompare = 0 Then 'if zero, it will remove it list1.removeitem(i) End If Next i
thanks everyone, works perfect
Code:Private Sub Command1_Click()
'''''''''' simple line to remove found item add text1.text to search
Dim i
For i = 0 To List1.ListCount - 1
If InStr(List1.List(i), Text1.Text) Then
List1.RemoveItem (i)
End If
Next i
End Sub
Private Sub Command2_Click()
'''''''''''''''''''''''''''''' use this to remove blank emty lines after it removed item
Dim i
For i = 0 To List1.ListCount - 1
If InStr(List1.List(i), Text1.Text) Then
List1.RemoveItem (i)
End If
Next i
Dim strCompare As String
Dim intCompare As Integer
For i = List1.ListCount - 1 To 0 Step -1
strCompare = List1.List(i)
intCompare = Len(Trim(strCompare))
If intCompare = 0 Then 'if zero, it will remove it
List1.RemoveItem (i)
End If
Next i
End Sub
Private Sub Form_Load()
List1.AddItem "holla"
List1.AddItem "glue"
List1.AddItem "aggass"
List1.AddItem "smile"
End Sub
add text1.text and list1
text1.text is to search and remove
cobraide - Please note that this thread is TEN YEARS OLD and that a solution was already found.