Listbox Transfer or records

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
I have two listboxes on a form. The first box is populated with many records. I want someone to be able to select as many records as they need, and press a button which will transfer those selected records into a second listbox, confirming they have selected.

Can anyone help with some basic code for this? Right now I am using the following code, but it only copies the very last record selected, instead of all of them...

Code:
Private Sub CommandButton2_Click()
    Dim bvalue As Boolean
    Dim sSum As String
    Dim x As Integer
    Dim rTotal As Range

    For x = 0 To UserForm1.lbxStock.ListCount - 1
        If UserForm1.lbxStock.Selected(x) Then
            sNode = UserForm1.lbxStock.List(x)
        End If
    Next x
    With Sheets("Lists (Do NOT Delete)")
        .Range("G2").Value = sNode
    End With
    
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Do you want to copy/move selected items to another listbox or a worksheet?

That code looks like it's doing the latter.

Whichever it is you need to move the copying/moving within the loop.
Code:
    For x = 0 To UserForm1.lbxStock.ListCount - 1

        If UserForm1.lbxStock.Selected(x) Then
                       
            ' to other listbox
            
            ListBox2.AddItem lbxStock.List(x)
            
            ' to worksheet
            rng.Value = lbxStock.List(x)
            Set rng = rng.Offset(1)
            
        End If
    Next x
 
Upvote 0
I am going to ask one more question on this one...

the current code only copies 1 column. If you had both listboxes said to 2 columns, how would you get the second column in there?
 
Upvote 0
In where?

Listbox or worksheet?

I could post code for both but I'd prefer to only do the relevant one.:)
 
Upvote 0
Well I did both of them anyway.

Note this will only work if the 2 listboxes have at least 2 columns.
Code:
Option Explicit
Private Sub CommandButton2_Click()
Dim bvalue As Boolean
Dim sSum As String
Dim x As Integer
Dim rTotal As Range
Dim rng As Range
 
    Set rng = Sheets("List (Do NOT Delete)").Range("G2")

    For x = 0 To UserForm1.lbxStock.ListCount - 1

        If UserForm1.lbxStock.Selected(x) Then

            ' to other listbox
 
            ListBox2.AddItem lbxStock.List(x)

            ListBox2.List(ListBox2.ListCount - 1, 1) = lbxStock.List(x, 1)
 
            ' to worksheet
            rng.Value = lbxStock.List(x)
            rng.Offset(, 1) = lbxStock.List(x, 1)

            Set rng = rng.Offset(1)
 
        End If
    Next x

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,916
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top