Hello everyone! This is my first time posting on ExcelForum, I am usually a lurker just checking stuff out - and usually it helps, but this time I'm truly stuck.
Basically what I am doing is... I have 2 listboxes in a userform. With 4 buttons.
Button One to move the selection (single or multiple items) from Listbox1 to Listbox2 and removing those items from Listbox1
Button Two to move all list items from Listbox1 to Listbox2 and clearing Listbox1.
Button Three to move the selection (single or multiple items) from Listbox2 to Listbox1 and removing those items from Listbox2.
Button Four to move all list items from Listbox2 to Listbox1 and clearing Listbox2.
The following is my code:
Private Sub UserForm_Initialize()
'Adds values to Listbox based on selected ranges.
With ListBox1
.AddItem Worksheets("Sheet1").Range("B12").Value
.AddItem Worksheets("Sheet1").Range("B13").Value
.AddItem Worksheets("Sheet1").Range("B14").Value
.AddItem Worksheets("Sheet1").Range("B15").Value
.AddItem Worksheets("Sheet1").Range("B16").Value
.AddItem Worksheets("Sheet1").Range("B17").Value
.AddItem Worksheets("Sheet1").Range("B18").Value
.AddItem Worksheets("Sheet1").Range("B19").Value
.AddItem Worksheets("Sheet1").Range("B20").Value
.AddItem Worksheets("Sheet1").Range("B21").Value
.AddItem Worksheets("Sheet1").Range("B22").Value
.AddItem Worksheets("Sheet1").Range("B23").Value
.AddItem Worksheets("Sheet1").Range("B24").Value
.AddItem Worksheets("Sheet1").Range("B25").Value
.AddItem Worksheets("Sheet1").Range("B26").Value
End With
ListBox1.MultiSelect = 2
ListBox2.MultiSelect = 2
End Sub
What I'm having problems with, is the following:
I want the order to remain as it is shown above. When moving back and forward between listboxes by pressing the button. Anyone know a solution to this without using a hidden sheet (i.e. only with coding)
Private Sub CommandButton8_Click()
Dim counter As Integer
counter = 0
'This code is used to add the selected item from the LEFT listbox to the one on the RIGHT
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
ListBox2.AddItem ListBox1.List(i)
End If
Next i
'This code below is used to remove the same selected item from the LEFT listbox, so there aren't 2 values of the same thing. And when
'transfering back, doesn't cause repeats.
'Counter is used to keep track of items that have been removed
For q = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(q - counter) = True Then
ListBox1.RemoveItem (q - counter)
counter = counter + 1
End If
Next q
End Sub
Private Sub CommandButton9_Click()
Dim counter2 As Integer
counter2 = 0
'Same as above, but from RIGHT listbox to LEFT
For m = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(m) = True Then
ListBox1.AddItem ListBox2.List(m)
End If
Next m
For n = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(n - counter2) Then
ListBox2.RemoveItem (n - counter2)
counter2 = counter2 + 1
End If
Next n
End Sub
I have spent so long on this... I just can't seem to solve it. I have fixed it for transfering all, as that is pretty simple. Clear Listbox and additem again etc...
Thanks guys, looking for help!
Bookmarks