I want to copy the selected items from listbox1 to listbox2.
I know how to copy all the items but not selected.
Private Sub Command3_Click()
For i = 1 To List1.ListCount
List2.AddItem i
Next
End Sub

Above Code will copy all the items,But I want to copy selected items.

you want to copy selected only, this will answer your question:

Private Sub Command3_Click()
List2.AddItem (List1.ListIndex)
End Sub

hope this helps...

hi.Jx_man, ur code is copying just 1 item, i want to copy multiple selected items.
I have tried the foll. code,Check it out where I m wrong-

Private Sub Command9_Click()
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then
List2.AddItem (List1.ListIndex)
End If
Next
End Sub

Your Mistake is
List2.AddItem (List1.ListIndex)

replace the above with
List2.AddItem (List1.List(i))

I want to copy the selected items from listbox1 to listbox2.
I know how to copy all the items but not selected.
Private Sub Command3_Click()
For i = 1 To List1.ListCount
List2.AddItem i
Next
End Sub

Above Code will copy all the items,But I want to copy selected items.

hi mansi, this is the completely code for you. make sure you have set the multiselect property of your listbox control to 2-Extended.

Option Explicit

Private Sub Command1_Click()
Dim i As Integer, c As Integer

If List1.ListCount = 0 Then
    MsgBox "No items found."
    Exit Sub
End If

c = 0
For i = 0 To List1.ListCount - 1 Step 1
    If List1.Selected(i) = True Then
        c = 1
        Exit For
    End If
Next i

If c = 0 Then
    MsgBox "Please select some items from the list."
    List1.SetFocus
    Exit Sub
ElseIf c = 1 Then
    For i = 0 To List1.ListCount - 1 Step 1
        If List1.Selected(i) = True Then
            List2.AddItem List1.List(i)
        End If
    Next i
End If
End Sub

Private Sub Form_Load()
Dim i As Integer

For i = 1 To 12 Step 1
    List1.AddItem MonthName(i)
Next i
End Sub

regards
Shouvik

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.