Run-time error '381'; on Listbox remove item - (Please Help)

K

K

I got code below which basically removes duplicates from Listbox2. It
works but the only problem is that I get error message saying "Run-
time error 381: Could not get the List property. Invalid property
array index" and when I click on "Debug" button it highlites the line
"If Me.ListBox2.List(i) = Me.ListBox2.List(x) Then". Please can any
friend help me on this that how can i correct this error.

******************code****************************
Private Sub CommandButton4_Click()
Dim i As Integer, x As Integer
For i = 0 To Me.ListBox2.ListCount - 1
For x = 0 To Me.ListBox2.ListCount - 1
If i <> x Then
If Me.ListBox2.List(i) = Me.ListBox2.List(x) Then
Me.ListBox2.RemoveItem (x)
Exit For
End If
End If
Next x
Next i
End Sub
 
G

GS

I see that you simply don't want duplicate items in Listbox2 AND not
delete items in Listbox1 once they've been added to Listbox2. If I
understand this correctly then what I think you should do is prevent
users from adding duplicate items from Listbox1.

Replace the code in the button to add items to Listbox2 with the
following:

<=====================>
' Transfers items from Listbox1 to Listbox2
' Does not allow duplicate items in Listbox2
Dim i As Long
For i = 0 To Me.ListBox2.ListCount - 1
If Me.ListBox1.Value = Me.ListBox2.List(i) Then Beep: Exit Sub
Next
Me.ListBox2.AddItem Me.ListBox1.Value
<=====================>

HTH
 
N

norie

I see that you simply don't want duplicate items in Listbox2 AND not
delete items in Listbox1 once they've been added to Listbox2. If I
understand this correctly then what I think you should do is prevent
users from adding duplicate items from Listbox1.

Replace the code in the button to add items to Listbox2 with the
following:

<=====================>
' Transfers items from Listbox1 to Listbox2
' Does not allow duplicate items in Listbox2
  Dim i As Long
  For i = 0 To Me.ListBox2.ListCount - 1
    If Me.ListBox1.Value = Me.ListBox2.List(i) Then Beep: Exit Sub
  Next
  Me.ListBox2.AddItem Me.ListBox1.Value
<=====================>

HTH

--
Garry

Free usenet access athttp://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc

If you want to populate a listbox with unique values try doing it when
you are initially populating or adding items to it.

Then you wouldn't need to use Remove
 
G

GS

norie explained on 11/19/2010 :
If you want to populate a listbox with unique values try doing it when
you are initially populating or adding items to it.

Then you wouldn't need to use Remove

?? That IS EXACTLY what the code I posted here does!
 
N

norie

norie explained on 11/19/2010 :








?? That IS EXACTLY what the code I posted here does!

--
Garry

Free usenet access athttp://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc- Hide quoted text -

- Show quoted text -

Garry

In the OP's code there only appears to be one listbox, perhaps that's
a typo.

I was kind of suggeting dealing with the duplicates before they go
anywhere near the listbox.

eg create a unique list using a dictionary, or some other method.
 
G

GS

Garry
In the OP's code there only appears to be one listbox, perhaps that's
a typo.

Actually, this thread started elsewhere and the OP did not continue
using the original. The original thread is titled "Listbox", and
contains the following:

"Hi all, I have two listboxes on the sheet and a command button. I
have macro on the button which moves items from Lisbox1 to Listbox2.
I need some kind of code that if i move item twice from Listbox1 to
Listbox2 then i should get message box saying that "You already have
moved this item" etc. Please can any friend can help me on this"

Not sure why all the different posts!<??>

I agree with you about preventing duplicate list items, and so the code
I posted checks Listbox2 to see that the selected item in Listbox1
hasn't already been added. If it has then the user gets beeped and no
action takes place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top