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
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
To move the entire contents use
VBA Code:
Me.Listbox2.List = Me.ListBox1.List
Me.ListBox1.Clear
 
Upvote 0
To move the entire contents use
VBA Code:
Me.Listbox2.List = Me.ListBox1.List
Me.ListBox1.Clear
Yeah that makes sense for the move all buttons. What could I use for the move selected?
This chunk of code:
VBA Code:
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
 
Upvote 0
To move individual lines try
VBA Code:
   Dim i As Long, j As Long
   
   With Me.ListBox1
      For i = .ListCount - 1 To 0 Step -1
         If .Selected(i) Then
            Me.Listbox2.AddItem .List(i)
            For j = 1 To 4
               Me.Listbox2.List(Me.Listbox2.ListCount - 1, j) = .List(i, j)
            Next j
         End If
      Next i
   End With
 
Upvote 0
To move individual lines try
VBA Code:
   Dim i As Long, j As Long
  
   With Me.ListBox1
      For i = .ListCount - 1 To 0 Step -1
         If .Selected(i) Then
            Me.Listbox2.AddItem .List(i)
            For j = 1 To 4
               Me.Listbox2.List(Me.Listbox2.ListCount - 1, j) = .List(i, j)
            Next j
         End If
      Next i
   End With
That works well, Thanks! Only issue is now there are duplicate rows. I want an entry to only exist once in both ListBoxes collectively. How could I clear the row in the first ListBox that was switched to the second ListBox? Would it be some iteration of Listbox1.removeitem?
 
Upvote 0
Oops, missed that bit, try
Rich (BB code):
   With Me.ListBox1
      For i = .ListCount - 1 To 0 Step -1
         If .Selected(i) Then
            Me.Listbox2.AddItem .List(i)
            For j = 1 To 4
               Me.Listbox2.List(Me.Listbox2.ListCount - 1, j) = .List(i, j)
            Next j
            .RemoveItem (i)
         End If
      Next i
   End With
 
Upvote 0
Oops, missed that bit, try
Rich (BB code):
   With Me.ListBox1
      For i = .ListCount - 1 To 0 Step -1
         If .Selected(i) Then
            Me.Listbox2.AddItem .List(i)
            For j = 1 To 4
               Me.Listbox2.List(Me.Listbox2.ListCount - 1, j) = .List(i, j)
            Next j
            .RemoveItem (i)
         End If
      Next i
   End With
Thanks!! Works great
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Oops, missed that bit, try
Rich (BB code):
   With Me.ListBox1
      For i = .ListCount - 1 To 0 Step -1
         If .Selected(i) Then
            Me.Listbox2.AddItem .List(i)
            For j = 1 To 4
               Me.Listbox2.List(Me.Listbox2.ListCount - 1, j) = .List(i, j)
            Next j
            .RemoveItem (i)
         End If
      Next i
   End With
One more question if you don't mind. You're very knowledgeable in this. This is ListBox4 that you helped me create. I'm looking to paste whatever value is located in the "Fee Override" Text box back onto the original data source to which these ListBoxes derive if the "Apply to Selected Accounts" button is pressed. I'd like the Fee Override to be pasted for each household that is shown in Listbox4. The Named range is called "Household" which does not include headers and is located on ("Sheet3").Range("I3:M300"). It's the same range that was used to fill ListBox3 and subsequently ListBox4. Household is located in column I and the Fee Override in Column M. I'd also like to remove the Fee override on the original data source for the households selected in ListBox4 if the "Revert Selected Accounts to Default Fee" Button is pressed.

Also, I have column 2 of the list range hidden on both ListBoxes if you were wondering.

Thank you!!!!!
 

Attachments

  • help.PNG
    help.PNG
    13.6 KB · Views: 50
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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