-
Dec 6th, 2021, 10:59 AM
#1
Thread Starter
New Member
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
-
Dec 6th, 2021, 12:34 PM
#2
Hyperactive Member
Re: Listbox - move from listbox 1 to listbox2
Originally Posted by cge2021
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
-
Dec 6th, 2021, 01:43 PM
#3
Thread Starter
New Member
Re: Listbox - move from listbox 1 to listbox2
Originally Posted by k_zeon
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.
-
Dec 6th, 2021, 09:23 PM
#4
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.
-
Dec 10th, 2021, 11:09 AM
#5
Thread Starter
New Member
Re: Listbox - move from listbox 1 to listbox2
Originally Posted by DataMiser
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|