+ Reply to Thread
Results 1 to 6 of 6

Extract unique values in a column that end in "ago"

Hybrid View

  1. #1
    Registered User
    Join Date
    01-19-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Extract unique values in a column that end in "ago"

    I have a column in Excel that reads like:

    1 year ago
    1990
    2 years ago
    1993
    1990
    1 year ago

    I want to extract into another column only those unique values that end in "ago", viz:

    1 year ago
    2 years ago

    I can do it using a macro, but this is inefficient as it creates 1024K blank lines in the spreadsheet when there are only about 100 filled lines.

    Any suggestions on how to do this with VB instead?

    TIA

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Extract unique values in a column that end in "ago"

    Hi blinks58
    try it (assumes that your data is in column A)
    Sub ertert()
    Application.ScreenUpdating = False
    Rows(1).Insert
    [a1] = "temp": [d1] = "temp": [d2] = "*ago"
    With Range("A1", Cells(Rows.Count, 1).End(xlUp))
        .AdvancedFilter 2, Range("D1:D2"), Range("B1"), True
    End With
    Rows(1).Delete: [d1] = ""
    Application.ScreenUpdating = True
    End Sub

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Extract unique values in a column that end in "ago"

    And example
    
    Sub test()
        Dim x
        With Range("a1", Range("a" & Rows.Count).End(xlUp))
            x = Filter(Evaluate("transpose(if(right(" & .Address & ",3)=""ago"",if(countif(offset(" & .Address & _
            ",0,0,row(1:" & .Rows.Count & "))," & .Address & ")=1," & .Address & ",char(2)),char(2)))"), Chr(2), 0)
        End With
        If UBound(x) <> -1 Then MsgBox Join$(x, vbLf)
    End Sub

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

    Re: Extract unique values in a column that end in "ago"

    Hi blinks58,

    Like the above suggestions, I'd say VBA is the way to go. That said, here's my attempt:

    Option Explicit
    Sub Macro1()
    
        'Written by Trebor76
        'Visit my website www.excelguru.net.au
    
        Dim clnMyUniqueValues As New Collection
        Dim rngCell As Range
        Dim strOutputCol As String
        
        strOutputCol = "B" ' Output column for unique values. Change to suit.
        
        Application.ScreenUpdating = False
        
        For Each rngCell In Range("A2", Range("A" & Rows.Count).End(xlUp)) 'Searches from cell A2 down to the last entry in column A. Change to suit.
        
            If InStr(rngCell, "ago") > 0 Then
            
                On Error Resume Next
                    clnMyUniqueValues.Add rngCell, CStr(rngCell)
                    If Err.Number = 0 Then
                        Cells(Cells(Rows.Count, strOutputCol).End(xlUp).Row + 1, strOutputCol).Value = rngCell
                    End If
                    Err.Clear
                On Error GoTo 0
                
            End If
        
        Next rngCell
        
        Application.ScreenUpdating = True
    
    End Sub
    HTH

    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

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Extract unique values in a column that end in "ago"

    AN ALTERNATIVE WAY USING FORMULA.

    Pl See attached file.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-19-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Extract unique values in a column that end in "ago"

    Thanks for your help, guys. All of your suggestions work, and a lot faster than my original macro. And none of those empty rows created, either!

    :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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