add items in listbox in the top position

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

add items in listbox in the top position

Post by sal21 »

I just have a listbox filled with ittems.
Now i want to add in the top of list item (first position of list) one other item value, How to?

ops!

But, check if already exist the item value in list, if yes, delete it before, to insert in the top new value

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: add items in listbox in the top position

Post by HansV »

Something like this:

Code: Select all

    Dim i As Long
    Dim strNewItem As String
    strNewItem = "..." ' to be filled in by you!
    With Me.ListBox1
        ' Loop through the items
        For i = 0 To .ListCount - 1
            ' If the new item already exists
            If .List(i) = strNewItem Then
                ' Delete the existing item
                .RemoveItem i
                ' And exit the loop
                Exit For
            End If
        Next i
        ' Add the new item in position 0 i.e. at the top
       .AddItem strNewItem, 0
    End With
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: add items in listbox in the top position

Post by sal21 »

HansV wrote:Something like this:

Code: Select all

    Dim i As Long
    Dim strNewItem As String
    strNewItem = "..." ' to be filled in by you!
    With Me.ListBox1
        ' Loop through the items
        For i = 0 To .ListCount - 1
            ' If the new item already exists
            If .List(i) = strNewItem Then
                ' Delete the existing item
                .RemoveItem i
                ' And exit the loop
                Exit For
            End If
        Next i
        ' Add the new item in position 0 i.e. at the top
       .AddItem strNewItem, 0
    End With
:thankyou:
eXCELLENT!