Results 1 to 5 of 5

Thread: Listbox - move from listbox 1 to listbox2

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2021
    Posts
    3

    Listbox - move from listbox 1 to listbox2

    Hi,

    I am very new at VBA. I am trying to create a userform with a couple of listboxes. The user selects the item they want and moves it over to listbox 2. I keep on encountering a runtime error -2147467259 (80004005) unspecified error. I believe it is related to the code clearing the selected item from listbox 1 to listbox 2. I am using a rowsource with a dynamic named range to populate listbox 1, not sure if that is related to the problem. Sometimes it works and sometimes it doesn't work. It is driving me batty, can someone please assist? First time posting so please let me know if additional info is needed or if there is a better way to provide the info.

    thank you

    Private Sub cmdMoveSelRight_Click()


    'Loop through the items
    For itemIndex = ListBox1.ListCount - 1 To 0 Step -1

    'Check if an item was selected.
    If ListBox1.Selected(itemIndex) Then

    'Move selected item to the right.
    ListBox2.AddItem ListBox1.List(itemIndex)

    'Remove selected item from the left.
    ListBox1.RemoveItem itemIndex


    End If

    Next itemIndex

    End Sub

  2. #2
    Hyperactive Member
    Join Date
    Nov 2011
    Posts
    498

    Re: Listbox - move from listbox 1 to listbox2

    Quote Originally Posted by cge2021 View Post
    Hi,

    I am very new at VBA. I am trying to create a userform with a couple of listboxes. The user selects the item they want and moves it over to listbox 2. I keep on encountering a runtime error -2147467259 (80004005) unspecified error. I believe it is related to the code clearing the selected item from listbox 1 to listbox 2. I am using a rowsource with a dynamic named range to populate listbox 1, not sure if that is related to the problem. Sometimes it works and sometimes it doesn't work. It is driving me batty, can someone please assist? First time posting so please let me know if additional info is needed or if there is a better way to provide the info.

    thank you

    Private Sub cmdMoveSelRight_Click()


    'Loop through the items
    For itemIndex = ListBox1.ListCount - 1 To 0 Step -1

    'Check if an item was selected.
    If ListBox1.Selected(itemIndex) Then

    'Move selected item to the right.
    ListBox2.AddItem ListBox1.List(itemIndex)

    'Remove selected item from the left.
    ListBox1.RemoveItem itemIndex


    End If

    Next itemIndex

    End Sub
    Works for me?

    Code:
    Private Sub cmdMoveSelRight_Click()
    
        Dim itemIndex As Integer
        'Loop through the items
        For itemIndex = ListBox1.ListCount - 1 To 0 Step -1
    
            'Check if an item was selected.
            If ListBox1.Selected(itemIndex) Then
    
                'Move selected item to the right.
                ListBox2.AddItem ListBox1.List(itemIndex)
    
                'Remove selected item from the left.
                ListBox1.RemoveItem itemIndex
    
    
            End If
    
        Next itemIndex
    
    End Sub
    
    
    
    Private Sub Form_Load()
        Dim x As Integer
        For x = 0 To 5
            ListBox1.AddItem x
        Next x
    End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2021
    Posts
    3

    Re: Listbox - move from listbox 1 to listbox2

    Quote Originally Posted by k_zeon View Post
    Works for me?

    Code:
    Private Sub cmdMoveSelRight_Click()
    
        Dim itemIndex As Integer
        'Loop through the items
        For itemIndex = ListBox1.ListCount - 1 To 0 Step -1
    
            'Check if an item was selected.
            If ListBox1.Selected(itemIndex) Then
    
                'Move selected item to the right.
                ListBox2.AddItem ListBox1.List(itemIndex)
    
                'Remove selected item from the left.
                ListBox1.RemoveItem itemIndex
    
    
            End If
    
        Next itemIndex
    
    End Sub
    
    
    
    Private Sub Form_Load()
        Dim x As Integer
        For x = 0 To 5
            ListBox1.AddItem x
        Next x
    End Sub

    I think my issue is, I'm trying to remove one item from a RowSource. I'm thinking that I cannot do that. I need to find a different way to populate my Listbox1.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Listbox - move from listbox 1 to listbox2

    So are you saying list1 is a bound listbox? If you so then you would need to remove the underlying item. Might be better to have the list unbound and just populate it in a loop so you can freely remove items from it without touching the source.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2021
    Posts
    3

    Re: Listbox - move from listbox 1 to listbox2

    Quote Originally Posted by DataMiser View Post
    So are you saying list1 is a bound listbox? If you so then you would need to remove the underlying item. Might be better to have the list unbound and just populate it in a loop so you can freely remove items from it without touching the source.
    Yes, bound listbox. That makes sense, thank you!

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