0
votes

I'm trying to populate a Listbox with unique values from Column P after populating the workbook through an activebutton. I've written the following syntax but for some reason it is only populating the ListBox with the first cells data but stops there.... Any ideas? I'm sure its something simple I'm missing as I'm new to VBA.

Dim Cellrng As Range
Dim Unique As New Collection
Dim Item As Range

On Error Resume Next
For Each Cellrng In ThisWorkbook.Worksheets("START").Range("P8:P" & lRow)
    Unique.Add Cellrng, CStr(Cell)
Next Cellrng
On Error GoTo 0

For Each Item In Unique
   ListBox1.AddItem Item
Next Item

enter image description hereenter image description here

First off, remove On Error Resume Next - now does it give you any errors? Next, you need to give lRow a value. That cannot be 0 or blank, as there is no "row 0" - Next, you never define Cell or give it a value either, so you have many problems. - braX
"This key is already associated with an element of this collection" - Highlights the "Unique.Add Cellrng, CStr (Cell)" line. I should have mentioned earlier, this block is running within a larger sub that has already given lRow the correct value. - bensasse
Now check all the values on that line. Make sure they are what you expect them to be. - braX