Results 1 to 12 of 12

Thread: Excel VBA Listbox with additional item number column

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Sep 2017
    Posts
    24
    Location

    Excel VBA Listbox with additional item number column

    Hi

    I am trying to populate a listbox that has 2 columns, the first includes the item data and the second just item number i.e.

    House 1
    Keys 2
    Car 3
    Trees 4

    This is actually listbox2 as I move items from listbox1 across to listbox2 with a command button


    Private Sub CommandButton2_Click()
     'Selected left to right
    Dim iCtr As Long
        For iCtr = 0 To Me.ListBox1.ListCount - 1
            If Me.ListBox1.Selected(iCtr) = True Then
                Me.ListBox2.AddItem Me.ListBox1.List(iCtr)
                Me.ListBox2.List(ListBox2.ListCount - 1, 1) = Me.ListBox2.ListCount
                End If
        Next iCtr
    For iCtr = Me.ListBox1.ListCount - 1 To 0 Step -1
            If Me.ListBox1.Selected(iCtr) = True Then
                Me.ListBox1.RemoveItem iCtr
            End If
        Next iCtr
    End Sub
    This works fine when adding but when removing I want the listbox to recalculate the items thus
    Keys removed

    House 1
    Car 2
    Trees 3

    It seems to work for last or second last item but not for any other item nearer top of list
    I have tried looping through items and reassigning the listboxcount and also tried listindex but listindex always gives zero?

    I do not want the items resorted in position just the item number

    Any help greatly appreciated

    thanks
    Last edited by KongUK; 09-25-2017 at 03:23 AM.

Posting Permissions

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