Move Data from one ListBox to another

reasem

New Member
Joined
Nov 15, 2019
Messages
38
I have a code that moves data from one ListBox to another using 4 buttons, but it only moves the data in the first column of the listbox. I'm looking for it to transfer the whole row in ListBox3 to ListBox4 and vice versa. I think my issue has to do with the AddItem and RemoveItem codes as they must only transfer one cell at a time.

Here's the code
VBA Code:
Private Sub BTN_moveAllLeft_Click()
    'Add the items to the other ListBox
For i = 0 To ListBox4.ListCount - 1
    ListBox3.AddItem ListBox4.List(i)
Next i

'Remove the items from the ListBox
ListBox4.Clear

'Remove items filled with RowSource
'ListBox2.RowSource=""
End Sub

Private Sub BTN_moveAllRight_Click()
    'Add items to the other ListBox
For i = 0 To ListBox3.ListCount - 1
    ListBox4.AddItem ListBox3.List(i)
Next i

'Remove the items from the ListBox
ListBox3.Clear

'Remove items filled with RowSource
'ListBox1.RowSource=""
End Sub

Private Sub BTN_MoveSelectedLeft_Click()
   'Loop through the items
For itemIndex = ListBox4.ListCount - 1 To 0 Step -1

    'Check if an item was selected.
    If ListBox4.Selected(itemIndex) Then

        'Move selected item to the right.
        ListBox3.AddItem ListBox4.List(itemIndex)

        'Remove selected item from the left.
        ListBox4.RemoveItem itemIndex

    End If

Next itemIndex
End Sub

Private Sub BTN_MoveSelectedRight_Click()
    'Loop through the items
For itemIndex = ListBox3.ListCount - 1 To 0 Step -1

    'Check if an item was selected.
    If ListBox3.Selected(itemIndex) Then

        'Move selected item to the right.
        ListBox4.AddItem ListBox3.List(itemIndex)

        'Remove selected item from the left.
        ListBox3.RemoveItem itemIndex

    End If

Next itemIndex
End Sub

Private Sub UserForm_Activate()

    
    Me.ListBox3.Clear
    Me.ListBox4.Clear
    
    ListBox3.ColumnCount = 5
    ListBox4.ColumnCount = 5
    
    Me.ListBox3.List = Worksheets("Sheet3").Range("Household").Value
    
   
    Me.ListBox3.MultiSelect = fmMultiSelectMulti
    Me.ListBox4.MultiSelect = fmMultiSelectMulti


End Sub
 
Easiest option is to put
=ROW() in N3 copied down & include that in the named range & therefore the listboxs.
You can then loop through the listbox & use that value to find the correct row for the Fee.
Ah gotcha. The households are all unique values so could I use that instead? Is there a code I would need to bind to those buttons?
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The households are all unique values so could I use that instead?
Not as easily. The point of putting the row number into the listbox, is that you can then use something along of the lines
VBA Code:
Private Sub CommandButton2_Click()
   Dim i As Long, j As Long
   
   With Me.ListBox1
      For i = .ListCount - 1 To 0 Step -1
         If .Selected(i) Then
            Range("M" & .List(i, 5)).Value = Me.TextBox1.Value
         End If
      Next i
   End With
End Sub
Saves having to find the correct row.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,525
Members
449,088
Latest member
RandomExceller01

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