It's as simple as
ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)
FOLLOWUP (From Comments)
I think I'm going to send the row to a worksheet and then add it to the other listbox from there.
I believe you are using a multicolumn listbox. In that case the above code will add only the first column to the 2nd listbox. You need to iterate through the rest of the columns to add the selected row from Listbox1
.
Let's say your userform looks like this. I created a small sample for you.
and the properties of the listboxes are set as below
and this is how your Sheet1
looks like.
Now put use this code in the Userform.
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), 0
j = ListBox2.ListCount - 1
For k = 1 To .ColumnCount - 1
ListBox2.List(j, k) = .List(i, k)
Next k
End With
End Sub
When you click select an item in the Listbox1
and press the command button, you will notice that the selected row from Listbox1
is successfully copied to Listbox2