Creating A List of Unique Values

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,612
Office Version
  1. 365
  2. 2016
Platform
  1. 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?

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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try:
VBA Code:
Sub UniqueList()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, dic As Object
    With ws_ifm
        .Unprotect
        v = .Range("A2", .Range("A" & .Rows.count).End(xlUp))
    End With
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v) To UBound(v)
        If Not dic.exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
        End If
    Next i
    ws_modlist.Range("L2").Resize(dic.count) = Application.Transpose(dic.keys)
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Disregard. I think I may have found the issue ...
Code:
For Each cl In .Range("A3:A" & lstrow)
Should be:
Code:
For Each cl In ws_ifm.Range("A3:A" & lstrow)

But now that it works, this code eventually crashes. Is 7000+ cells of data too much for this code to compile uniques?[/code]
 
Upvote 0
Did you try the macro I suggested in Post #2? Change the range to suit your needs.
 
Upvote 0
Try:
VBA Code:
Sub UniqueList()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, dic As Object
    With ws_ifm
        .Unprotect
        v = .Range("A2", .Range("A" & .Rows.count).End(xlUp))
    End With
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v) To UBound(v)
        If Not dic.exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
        End If
    Next i
    ws_modlist.Range("L2").Resize(dic.count) = Application.Transpose(dic.keys)
    Application.ScreenUpdating = True
End Sub
This worked far better than my previous attempt Mumps, thank you so much!
 
Upvote 0

Forum statistics

Threads
1,224,760
Messages
6,180,816
Members
452,996
Latest member
nelsonsix66

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