Userform

G

Guest

Dear all,

Please advise what's wrong with the code below to remove duplicates from a
list to a listbox in an userform. A warning dialog box stated "variable not
defined" to a word "Item"

Sub RemoveDuplicates1()
Dim AllCells As Range, Cell As Range
Dim NoDupes As New Collection

On Error Resume Next
For Each Cell In Range("A1:A54")
NoDupes.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0

For Each Item In NoDupes
UserForm1.ListBox1.AddItem Item
Next Item

UserForm1.Show
Unload UserForm1
End Sub

Thanks.
 
A

Andy Pope

Hi,

You have Option Explicit set at the top of your code module, which is
good. This requires you to declare ALL variables.

You have not declare the variable Item.

Cheers
Andy
 
B

Bob Phillips

Item is not a good word for a variable either. I haven't tested if it is a
problem, but as it is part of VBA syntax it should be avoided.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
B

Bob Phillips

Many do Andy, but being part of the syntax means that we cannot guarantee
they always will.

Bob
 

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