Data from Listbox1 to ListBox2

Status
Not open for further replies.

mrsec

Board Regular
Joined
Jan 28, 2016
Messages
54
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I have userform that has 2 listbox's,
Listbox1 shows the data from search and by Adding command button i can select single row and transfer/move it to Listbox2
But for some reason everytime i transfer a new row from LB1 to LB2 the first one that i transfer is been overwritten by the later.
My code
VBA Code:
Private Sub CommandButton2_Click()
    Dim i As Long
    ReDim b(1, 1 To 16)
With ListBox2
    .ColumnCount = 16
    .List = b
    .Clear
End With

    For i = Me.ListBox1.ListCount - 1 To 0 Step -1
        If Me.ListBox1.Selected(i) = True Then
            Me.ListBox2.AddItem Me.ListBox1.List(i)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 1) = Me.ListBox1.List(i, 1)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 2) = Me.ListBox1.List(i, 2)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 3) = Me.ListBox1.List(i, 3)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 4) = Me.ListBox1.List(i, 4)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 5) = Me.ListBox1.List(i, 5)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 6) = Me.ListBox1.List(i, 6)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 7) = Me.ListBox1.List(i, 7)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 8) = Me.ListBox1.List(i, 8)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 9) = Me.ListBox1.List(i, 9)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 10) = Me.ListBox1.List(i, 10)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 11) = Me.ListBox1.List(i, 11)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 12) = Me.ListBox1.List(i, 12)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 13) = Me.ListBox1.List(i, 13)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 14) = Me.ListBox1.List(i, 14)
            Me.ListBox2.List(Me.ListBox2.ListCount - 1, 15) = Me.ListBox1.List(i, 15)
            Me.ListBox1.RemoveItem i
     Exit For
        End If
    Next
End Sub
 

Attachments

  • listbox.jpg
    listbox.jpg
    100.6 KB · Views: 16

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Cannot test it, but I seem you have to remove that "-1"; also you may use a loop rather then repeating 15 times the same instruction.
Thus:
VBA Code:
    For i = Me.ListBox1.ListCount - 1 To 0 Step -1
        If Me.ListBox1.Selected(i) = True Then
            Me.ListBox2.AddItem Me.ListBox1.List(i)
            For j = 1 To 15
                Me.ListBox2.List(Me.ListBox2.ListCount, j) = Me.ListBox1.List(i, j)
            Next j
            Me.ListBox1.RemoveItem i
            Exit For
        End If
    Next
Bye
 
Upvote 0
Cannot test it, but I seem you have to remove that "-1"; also you may use a loop rather then repeating 15 times the same instruction.
Thus:
VBA Code:
    For i = Me.ListBox1.ListCount - 1 To 0 Step -1
        If Me.ListBox1.Selected(i) = True Then
            Me.ListBox2.AddItem Me.ListBox1.List(i)
            For j = 1 To 15
                Me.ListBox2.List(Me.ListBox2.ListCount, j) = Me.ListBox1.List(i, j)
            Next j
            Me.ListBox1.RemoveItem i
            Exit For
        End If
    Next
Bye
Thank you.yeah im having a hard time with the loop as my row contains 10+ columns.I tried removing "-1" but it doesnt work.
 
Upvote 0
Try with your original code, not the loop; and please specify what happens.
 
Upvote 0
No, remove -1 in these instructions:
Me.ListBox2.List(Me.ListBox2.ListCount - 1, xx) = Me.ListBox1.List(i, xx)

Bye
 
Upvote 0
No, remove -1 in these instructions:
Me.ListBox2.List(Me.ListBox2.ListCount - 1, xx) = Me.ListBox1.List(i, xx)

Bye
image attached for the error
 

Attachments

  • errorminusone.jpg
    errorminusone.jpg
    45.7 KB · Views: 8
Upvote 0
On which instruction? The one highlighted while in debug mode)?
(or confirm the proble is solved using dmt32 code on "the other" discussion)
Bye
 
Upvote 0
On which instruction? The one highlighted while in debug mode)?
(or confirm the proble is solved using dmt32 code on "the other" discussion)
Bye
image attached.
 

Attachments

  • high.jpg
    high.jpg
    106.8 KB · Views: 9
Upvote 0
Is the problem fixed using dmt32 code in "the other" discussion?
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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