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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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>
 
Upvote 0
Thank you Ivan that worked, now could I trouble you to explain it so that I can understand

Derrick
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

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