0

I have a list of data (with a header) on a sheet called "DataSheet".

I'm looking to get a list of all unique values in Column C (ignoring the header) on DataSheet and put them vertically into Column 16 (starting at Row 2) on a sheet called "Divide".

I originally used:

Sheets("Divide").Cells(2, 16).Formula2 = "=UNIQUE(FILTER(Ledger!C2:C25000,DataSheet!C2:C25000<>" & Chr(34) & Chr(34) & "))"

to put a Dynamic Range formula into the first cell (2,16) on the Divide Sheet, but found that Formula2 isn't backwards compatible with Excel 2019 and prior.

Is there another way to do this with VBA?

Thanks!

0

2 Answers 2

0

First, create a dictionary object to store unique values, then loop through Column C of DataSheet (starting at row 2 to skip header), then add each non-empty value to the dictionary (dictionaries automatically handle uniqueness), finally transfer the unique values from the dictionary to Column 16 of the Divide sheet

Here is an example of dictionary usage: dict(value) = 1 adds unique values automatically, dict.Keys contains all unique value, and no need to check for duplicates

Sub ExtractUniqueValues()
    Dim dict As Object
    Dim cell As Range
    Dim i As Long
    
    Set dict = CreateObject("Scripting.Dictionary")
    
    For Each cell In Sheets("DataSheet").Range("C2:C" & Sheets("DataSheet").Cells(Rows.Count, "C").End(xlUp).Row)
        If Not IsEmpty(cell) Then dict(cell.Value) = 1
    Next
    
    ' Output
    i = 2
    For Each key In dict.Keys
        Sheets("Divide").Cells(i, 16) = key
        i = i + 1
    Next
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Alex! Unfortunately I'm not familiar with using dictionary objects. Any chance that you could provide some example code?
Yes ill edit it with an example
Works perfectly, thanks!
thats awesome! glad it worked
0

you could use Excel built in RemoveDuplicates function

Sub GetUniqueValues()
    
    Dim dataRng As Range
        Set dataRng = Sheets("DataSheet").Range("C2", Sheets("DataSheet").Cells(Sheets("DataSheet").Rows.Count, "C").End(xlUp)) ' store data range
        With Sheets("Divide").Cells(2, 16).Resize(dataRng.Rows.Count)  ' reference output sheet range
            .Value = dataRng.Value ' paste data values 
            .RemoveDuplicates Columns:=1, Header:=xlNo ' remove duplicates
        End With
    
End Sub

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.