Moving row from Listbox1 to Listbox2

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
Hi good morning, hope you can help me please, i have a Userform1 which has a listbox1 and listbox2 in it, i have a command button for listbox1, for when this is clicked i want it to remove the row selected and move it into listbox2 i have the code below but it is not working, i have a background of yellow on a line of code 'Me.ListBox2.AddItem Me.ListBox1.List(iCnt)'. hope you can help me please.
VBA Code:
Private Sub CommandButton8_Click()
    
    'Variable Declaration
    Dim iCnt As Integer
    
    'Move Items from ListBox1 to ListBox2
    For iCnt = 0 To Me.ListBox1.ListCount - 1
        Me.ListBox2.AddItem Me.ListBox1.List(iCnt)
    Next iCnt
    
    'Clear ListBox1 After moving Items from ListBox1 to ListBox2
    Me.ListBox1.Clear
    
End Sub
 
I mentioned in post 14 that I also get that error but I am looking into a fix, I dont understand currently what is causing it. It seems to be something to do with the log files. I will revert when i find a fix, or maybe someone else here will understand why the error is there?
Thank you hope you can find the issue
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try replacing the below sub:
VBA Code:
Private Sub CommandButton8_Click()
    Dim i As Long, itm As Long
 
    For i = 0 To Me.ListBox2.ListCount - 1
        If Me.ListBox2.Selected(i) Then
            itm = i + 2
            Exit For
        End If
    Next i
 
    sh1.Rows(itm).Cut sh2.Range("A" & lrB + 1)
    sh1.Rows(itm).Delete
    Application.CutCopyMode = False
 
    ws1Rng
    ws2Rng
End Sub

With this one:
VBA Code:
Private Sub CommandButton8_Click()
    Dim i As Long, itm As Long
   
    Application.ScreenUpdating = False
   
    For i = 0 To Me.ListBox2.ListCount - 1
        If Me.ListBox2.Selected(i) Then
            itm = i + 2
            Exit For
        End If
    Next i
 
    sh1.Rows(itm).Copy
    sh2.Range("A" & lrB + 1).PasteSpecial xlValues
    sh1.Rows(itm).ClearContents
    Application.CutCopyMode = False
    sh1.Range("A1:J" & sh1.Range("A" & Rows.Count).End(xlUp).Row).Sort Key1:=sh1.Range("A1"), Order1:=xlAscending, Header:=xlYes
   
    Application.ScreenUpdating = True

    ws1Rng
    ws2Rng
End Sub
 
Last edited:
Upvote 0
Solution
Just edited a bit of the above code from:
VBA Code:
Key1:=Range("A1")
To:
VBA Code:
Key1:=sh1.Range("A1")

Might be worth having a look at that part if you have already taken a copy of the code.
 
Upvote 0
Just edited a bit of the above code from:
VBA Code:
Key1:=Range("A1")
To:
VBA Code:
Key1:=sh1.Range("A1")

Might be worth having a look at that part if you have already taken a copy of the code.
This works perfectly now thank you so much for your help much appreciated.
 
Upvote 0
@Patriot2879
Please refer to my recent comments to you starting here.
Again in this thread you marked your own post (#24) as the solution when the solution is actually @Georgiboy's code (either post #22 or the with the slight adjustment in post #23). I have changed the 'Mark as solution' to #22 as that is the gist of the solution.

In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,029
Members
449,092
Latest member
ikke

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