Tow listbox in One Userform

mrsec

Board Regular
Joined
Jan 28, 2016
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello,I have two listboxs in one userform,what i was trying to achieve is to move the row ive selected from Listbox1 to listbox2 which it works, problem is it only copies the first column of the data.

I have a total of 16 columns in my worksheet from B to Q. And the listbox1 only shows 4 columns of the data which is what i wanted to(minimized the data information).But when si run the code of moving from one listbox to another it only shows the first column of data ,what am i missing?
I have two command button to move around the data from listbox1 to listbox2 and vice versa.
Code from Listbox1 to ListBox2
VBA Code:
Private Sub CommandButton2_Click()

    Dim i As Long

    For i = Me.ListBox1.ListCount - 1 To 0 Step -1
        If Me.ListBox1.Selected(i) = True Then
            Me.ListBox3.AddItem Me.ListBox1.List(i)
            Me.ListBox1.RemoveItem i
            Exit For
        End If
    Next

End Sub

Code for listbox 2 to listbox 1(return of data)
VBA Code:
Private Sub CommandButton3_Click()


    Dim i As Long

    For i = Me.ListBox3.ListCount - 1 To 0 Step -1
        If Me.ListBox3.Selected(i) = True Then
            Me.ListBox1.AddItem Me.ListBox3.List(i)
            Me.ListBox3.RemoveItem i
            Exit For
        End If
    Next

End Sub
 

Attachments

  • listbox1tolistbox2.jpg
    listbox1tolistbox2.jpg
    84.9 KB · Views: 19

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Does the new question (see link above) supersede this one?

Bye
 
Upvote 0
Does the new question (see link above) supersede this one?

Bye
oh yeah,was searching how to remove the post,but cant find any delete option.
 
Upvote 0
Whilst your post looks like a duplicate / continuation of your earlier thread & MOD may have views on it, see if this update to your code helps

VBA Code:
Private Sub CommandButton2_Click()
    MoveSelection Me.ListBox1, Me.ListBox3
End Sub

Private Sub CommandButton3_Click()
    MoveSelection Me.ListBox3, Me.ListBox1
End Sub


Private Sub MoveSelection(ByVal FromListBox As Object, ByVal ToListBox As Object)
    Dim r       As Long, c As Long
    Dim Form    As Object
    
    On Error Resume Next
    
    Set Form = FromListBox.Parent
    
    With FromListBox
        For r = .ListCount - 1 To 0 Step -1
            If .Selected(r) = True Then
                ToListBox.AddItem .List(r, 0)
                For c = 1 To .ColumnCount - 1
                        ToListBox.List(ToListBox.ListCount - 1, c) = .List(r, c)
                Next c
                .RemoveItem r
            End If
        Next r
    End With
    Me.CommandButton2.Enabled = Form.ListBox1.ListCount > 0
    Me.CommandButton3.Enabled = Form.ListBox3.ListCount > 0
    On Error GoTo 0
End Sub

Solution untested & I am heading off to play football with my grandson so will have to leave you to adjust to meet project need as required.

Dave
 
Upvote 0
Solution
Whilst your post looks like a duplicate / continuation of your earlier thread & MOD may have views on it, see if this update to your code helps

VBA Code:
Private Sub CommandButton2_Click()
    MoveSelection Me.ListBox1, Me.ListBox3
End Sub

Private Sub CommandButton3_Click()
    MoveSelection Me.ListBox3, Me.ListBox1
End Sub


Private Sub MoveSelection(ByVal FromListBox As Object, ByVal ToListBox As Object)
    Dim r       As Long, c As Long
    Dim Form    As Object
   
    On Error Resume Next
   
    Set Form = FromListBox.Parent
   
    With FromListBox
        For r = .ListCount - 1 To 0 Step -1
            If .Selected(r) = True Then
                ToListBox.AddItem .List(r, 0)
                For c = 1 To .ColumnCount - 1
                        ToListBox.List(ToListBox.ListCount - 1, c) = .List(r, c)
                Next c
                .RemoveItem r
            End If
        Next r
    End With
    Me.CommandButton2.Enabled = Form.ListBox1.ListCount > 0
    Me.CommandButton3.Enabled = Form.ListBox3.ListCount > 0
    On Error GoTo 0
End Sub

Solution untested & I am heading off to play football with my grandson so will have to leave you to adjust to meet project need as required.

Dave
Thanks.:)
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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