+ Reply to Thread
Results 1 to 5 of 5

Macro to get unique values from range of cells?

Hybrid View

  1. #1
    Registered User
    Join Date
    04-10-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Macro to get unique values from range of cells?

    Hi guys, got a question relating to vba. I've done some coding so know roughly what I need to do, just don't have that much experience in excel/ vb.

    So, what I want to do is get all the unique values from a range of cells and print them in another range of cells, one value per cell. For example, this would be the result:
    Original List Result of Macro
    a a
    a b
    b c
    c
    a
    I assume this would be done by putting the values into an array and then printing that array with some kind of loop (but please correct me if I'm wrong, that guess is based off other code applicaitons).

    If possible it'd be great if the macro ignored cells with commas in them e.g. if the contents of a cell was "a,b" it would just skip that cell.

    Thanks for any help in advance, and sorry for the newbie question!

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to get unique values from range of cells?

    Maybe:

    Sub wyndham29()
    Dim d As Object, c As Variant, i As Long, lr As Long
    Set d = CreateObject("Scripting.Dictionary")
    lr = Cells(Rows.Count, 1).End(xlUp).row
    c = Range("A2:A" & lr)
    For i = 1 To UBound(c, 1)
      d(c(i, 1)) = 1
    Next i
    Range("B2").Resize(d.Count) = Application.Transpose(d.keys)
    End Sub
    Results from Column A to Column B.

  3. #3
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro to get unique values from range of cells?

    Another one.. much the same as JHD's..

    Private Sub CommandButton1_Click()
        Dim x, Z
        With CreateObject("Scripting.Dictionary")
            For Each x In Application.Transpose(Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row))
                Z = .Item(x)
            Next x
            [B1].Resize(.Count).Value = Application.Transpose(.Keys)
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    04-10-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to get unique values from range of cells?

    Thanks guys, will try out now!

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Macro to get unique values from range of cells?

    Hi wyndham29,

    Here's my attempt which also takes into account your request to ignore cells with commas and has some commentary about what the code is doing:

    Option Explicit
    Sub Macro1()
    
        Dim rngCell As Range
        Dim clnUniqueValues As New Collection
        
        Application.ScreenUpdating = False
        
        For Each rngCell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row) 'Starts at roe Row 2 of Col. A. Change to suit.
            If InStr(rngCell, ",") = 0 Then 'Only run the following code if the cell doesn't have a comma in it.
                On Error Resume Next 'Turn error reporting off as we're not interested in the 'Run-time error '457' This key is already associated with an element of this collectopn' error message as we only want unique entries anyway.
                    clnUniqueValues.Add rngCell.Value, CStr(rngCell.Value)
                    If Err.Number = 0 Then
                        Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = rngCell 'Puts each unique item in Col. A into the next available Row of Col. B. Change to suit.
                    End If
                    Err.Clear
                On Error GoTo 0
            End If
        Next rngCell
        
        Application.ScreenUpdating = True
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formula or VBA - Comparison CSV Values between 3 cells for used range. Find unique values.
    By grphillips in forum Excel Programming / VBA / Macros
    Replies: 36
    Last Post: 06-09-2014, 02:12 PM
  2. [SOLVED] Find unique values from range ( populated cells only) & not count zeros or NA errors VBA
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2014, 06:50 AM
  3. [SOLVED] find unique values in a range that contains text and empty cells
    By bombicci in forum Excel General
    Replies: 4
    Last Post: 07-04-2012, 09:17 AM
  4. Replies: 3
    Last Post: 09-20-2011, 05:05 PM
  5. force unique values in a range of cells?
    By keithb in forum Excel General
    Replies: 1
    Last Post: 08-08-2005, 04:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1