Why does this code not work?

technix

Active Member
Joined
May 19, 2002
Messages
326
Code:
Private Sub »»_Click()
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
ListBox2.AddItem (ListBox1.List(i))
End If
Next i
For j = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(j) = True Then
ListBox1.RemoveItem (ListBox1.List(j))
End If
Next j
End Sub

The part here

Code:
Private Sub »»_Click()
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
ListBox2.AddItem (ListBox1.List(i))
End If
Next i

This will take the multi selected items and will add them to listbox2

This code is where i am having problems

Code:
For j = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(j) = True Then
ListBox1.RemoveItem (ListBox1.List(j))
End If
Next j
End Sub

What I want to happen is as soon as the items selected were added to the listbox2 they would be deleted from listbox1

Derrick
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
Try;

<pre/>
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
ListBox2.AddItem (ListBox1.List(i))
End If
Next i

For j = ListBox1.ListCount - 1 To 0 Step -1
If ListBox1.Selected(j) = True Then
ListBox1.RemoveItem (j)
End If
Next j

</pre>
 

technix

Active Member
Joined
May 19, 2002
Messages
326
Thank you Ivan that worked, now could I trouble you to explain it so that I can understand

Derrick
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
On 2002-08-30 23:31, technix wrote:
Thank you Ivan that worked, now could I trouble you to explain it so that I can understand

Derrick

Derrick
You nearly had the 1st part you just needed to have the index and NOt the actual selection input.
The last bit of your code you needed to step
down from the Actual Max index count as changing the Listindex via deleting also changes the index...if that makes sense.
ie start from the Last entry and work down and not from the 1st as this will change when you remove it.
 

Forum statistics

Threads
1,143,707
Messages
5,720,386
Members
422,282
Latest member
psunith

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
Top