Ok!! I am updating the above answer of Mr. Maco.
Private Sub UserForm_Initialize()
'~~> Adding Sample Data to listbox 1
ListOne.List = ThisWorkbook.Sheets(1).Range("A1:A8").Value 'Here ListOne is the name of ListBox1
End Sub
Private Sub CommandButton1_Click()
Dim iIndex
Dim i As Long, j As Long, k As Long
With ListBox1
i = ListOne.ListIndex
ListTwo.AddItem ListOne.List(i, 0) 'ListTwo is the name of ListBox2
j = ListTwo.ListCount - 1
For k = 1 To ListOne.ColumnCount - 1
ListTwo.List(j, k) = .List(i, k)
Next k
ListOne.RemoveItem (i) ' Add here the reference Name i.e. ListOne
End With
End Sub
I hope, this should work for you.
Function of a add button in vba userform
Question
I have two list boxes.Listbox_1 displays list of food items and listbox_2 is empty. upon clicking a button the selected item from box1 should be moved to box2. i dont know where to start. Can any one help me?
Solution
OTHER TIPS
Between line:15 and line:16 of the sample code (stackoverflow.com/questons/19064043/...),
insert this line.
.RemoveItem(i)
example
Private Sub UserForm_Initialize()
'~~> Adding Sample Data to listbox 1
ListBox1.List = ThisWorkbook.Sheets(1).Range("A1:E3").Value
End Sub
Private Sub CommandButton1_Click()
Dim iIndex
Dim i As Long, j As Long, k As Long
With ListBox1
i = .ListIndex
ListBox2.AddItem .List(i, 0)
j = ListBox2.ListCount - 1
For k = 1 To .ColumnCount - 1
ListBox2.List(j, k) = .List(i, k)
Next k
.RemoveItem(i)
End With
End Sub
List Boxes keeps the value with index numbers.
On CommandButtom_Click, access the index of that value using ListBox.ListIndex.
Then access the value of that index and put value in the Other List. And after adding that value in the 2nd List remove the value from 1st list using that index number.
I am just giving you a direction, I hope it would help you.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow