Ark68
Well-known Member
- Joined
- Mar 23, 2004
- Messages
- 4,612
- Office Version
- 365
- 2016
- Platform
- Windows
I am using this code (thanks @pboltonchina) to create a list of all the unique values captured from column A in ws_ifm , to column L in ws_modlist starting at L2.
I get no errors, however, I am only getting one value listed, at L2 when there should be dozens in this list. Where have I gone wrong? Perhaps I misunderstood the results of this code?
I get no errors, however, I am only getting one value listed, at L2 when there should be dozens in this list. Where have I gone wrong? Perhaps I misunderstood the results of this code?
VBA Code:
Sub UniqueList()
' @pboltonchina https://www.mrexcel.com/board/threads/vba-create-unique-list-from-range-of-data.611558/
Dim uniqueArray() As Variant
Dim count As Integer
Dim notUnique As Boolean
Dim lstrow As Long, ml_lstrow As Long
Dim i As Long
ReDim uniqueArray(0) As Variant
uniqueArray(0) = Range("A1")
count = 0
Stop
Dim cl As Range
With ws_ifm
.Unprotect
.AutoFilterMode = False
lstrow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Debug.Print "Last row in FM: " & lstrow
End With
With ws_modlist
'prepare model list destination
If .AutoFilterMode Then .AutoFilter = False
ml_lstrow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
If ml_lstrow > 1 Then .Range("L2:L" & ml_lstrow).ClearContents
For Each cl In .Range("A3:A" & lstrow)
notUnique = False
For i = LBound(uniqueArray) To UBound(uniqueArray)
If (cl.Value = uniqueArray(i)) Then
notUnique = True
Exit For
End If
Next i
If notUnique = False Then
count = count + 1
ReDim Preserve uniqueArray(count) As Variant
uniqueArray(UBound(uniqueArray)) = cl.Value
End If
Next cl
Stop
For i = LBound(uniqueArray) To UBound(uniqueArray)
.Range("L2").Offset(i, 0) = uniqueArray(i)
Next i
End With
End Sub