I have a form with two ListBoxes (ListBox1 & ListBox2). The code to initialize ListBox1 is below, where a named range (SheetA) in entered into cell A1:
Private Sub UserForm_Initialize()
‘Prepare ListBox
With listBox1
.ColumnCount = 3
.ColumnWidths = "50;100;100"
.RowSource = ""
End With
‘Generate list from named range
Dim SheetA As Range
Set SheetA = Range("A1") 'This is a named range
Dim counter As Long
Dim totalRows As Long
totalRows = Range(SheetA).Rows.Count
counter = 0
Do
With Me.listBox1
counter = counter + 1
''create a new row with Additem
.AddItem Range(SheetA)(counter, 1).Value
''add item in second column of a row
.List(.ListCount - 1, 1) = Range(SheetA)(counter, 1).Offset(0, 1).Value
''add item in third column of a row
.List(.ListCount - 1, 2) = Range(SheetA)(counter, 1).Offset(0, 2).Value
End With
Loop Until counter = totalRows
End Sub
There is a CommandButton to move selection from ListBox1 to ListBox2, code below:
Private Sub cmdMoveSelRight_Click()
Dim iCnt As Integer
'Move Selected Items from Listbox1 to Listbox2
For iCnt = 0 To Me.listBox1.ListCount - 1
If Me.listBox1.Selected(iCnt) = True Then
Me.listBox2.AddItem Me.listBox1.List(iCnt)
End If
Next
For iCnt = Me.listBox1.ListCount - 1 To 0 Step -1
If Me.listBox1.Selected(iCnt) = True Then
Me.listBox1.RemoveItem iCnt
End If
Next
End Sub
The first code works fine and generates a 3 column list. But the second code only transfers the first column into ListBox2. How can the code be altered to transfer all 3 columns from ListBox1 to ListBox2? Thanks Sandy
Bookmarks