Consulting

Results 1 to 3 of 3

Thread: vba userform move listbox1 item to listbox2

  1. #1
    Banned VBAX Contributor
    Joined
    Aug 2017
    Posts
    144
    Location

    vba userform move listbox1 item to listbox2

    Hi Team,

    I am trying to move listbox1 value to listbox2.
    code is working but it is moving all item. in case if select bottom value.
    top to bottom is working.
    attached sample code and workbook




    Private Sub CommandButton2_Click()
    MoveSelectedItem Me.ListBox1, Me.ListBox2
    End Sub


    Private Sub CommandButton3_Click()
    MoveSelectedItem Me.ListBox2, Me.ListBox1
    End Sub


    Private Sub MoveAllData(xSource As Object, xDestination As Object)
    Dim i As Long
    For i = xSource.ListCount - 1 To 0 Step -1


    xDestination.AddItem xSource.List(i, 0)
    xDestination.List(xDestination.ListCount - 1) = xSource.List(i)
    xSource.RemoveItem (i)


    Next
    End Sub


    Private Sub CommandButton4_Click()
    If Me.ListBox1.ListIndex < 0 Then
    MoveAllData Me.ListBox2, Me.ListBox1
    Else
    MoveAllData Me.ListBox1, Me.ListBox2
    End If
    End Sub



    Thanks
    mg
    Attached Files Attached Files

  2. #2
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    Hi,
    your request is unclear.
    you seem to be saying that you wish to move the 'selected item' from one listbox into a second listbox, or at least control which parts of the list are transfered.

    it seems that you will need to use the listindex.
    you can get the index of the selected item
    you can also loop through all items in a listindex and use logic to control what happens at each listitem.

    hope this gives you something

    Werafa
    Remember: it is the second mouse that gets the cheese.....

  3. #3
    Change your move code to:
    Private Sub MoveSelectedItem(xSource As Object, xDestination As Object)
    Dim i As Long
    For i = 0 To xSource.ListCount - 1
     If xSource.Selected(i) Then
        xDestination.AddItem xSource.List(i, 0)
        xDestination.List(xDestination.ListCount - 1, 1) = xSource.List(i, 1)
     End If
    Next
    For i = xSource.ListCount - 1 To 0 Step -1
     If xSource.Selected(i) Then
        xSource.RemoveItem (i)
     End If
    Next
    End Sub
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

Posting Permissions

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