+ Reply to Thread
Results 1 to 7 of 7

sort column based on number of duplicate values

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    sort column based on number of duplicate values

    Hello all,

    I have countries listed in Column D with many duplicates. My goal is to sort Column D, from highest to lowest, based on the number of duplicates:

    So, this:

    Country
    US
    US
    US
    US
    UK
    UK
    Italy
    Italy
    Italy
    Italy
    Italy
    Italy

    would become this:
    Italy
    Italy
    Italy
    Italy
    Italy
    Italy
    US
    US
    US
    US
    UK
    UK

    The following code works, but is very slow (I have 20,000+ rows of data). Are there any faster alternatives?

    (Note: I have data in column A which I use to define the last row of data -LR-)

    Sub Macro1()
    '
    ' Macro1 Macro
    
    Dim LR As Long
    
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Columns("E:E").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("E2").FormulaR1C1 = "=COUNTIF(C[-1],RC[-1])"
    Range("E2").AutoFill Destination:=Range("E2", "E" & LR)
        
    ActiveWorkbook.Worksheets("sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("sheet1").Sort.SortFields.Add Key:=Range( _
         "E2", "E" & LR), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
         xlSortNormal
    With ActiveWorkbook.Worksheets("sheet1").Sort
         .SetRange Range("A1", "Z" & LR)
         .Header = xlYes
         .MatchCase = False
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin
         .Apply
    End With
    
    
    
    
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: sort column based on number of duplicate values

    Try this

    Sub Macro1()
    '
    ' Macro1 Macro
    
    Dim LR As Long
    Application.Calculation = xlManual
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Columns("E:E").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("E2").FormulaR1C1 = "=COUNTIF(C[-1],RC[-1])"
    Range("E2").AutoFill Destination:=Range("E2", "E" & LR)
    ActiveSheet.Calculate
        
    ActiveWorkbook.Worksheets("sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("sheet1").Sort.SortFields.Add Key:=Range( _
         "E2", "E" & LR), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
         xlSortNormal
    With ActiveWorkbook.Worksheets("sheet1").Sort
         .SetRange Range("A1", "Z" & LR)
         .Header = xlYes
         .MatchCase = False
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin
         .Apply
    End With
    
    Application.Calculation = xlAutomatic
    
    
    End Sub
    Elegant Simplicity............. Not Always

  3. #3
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    Re: sort column based on number of duplicate values

    Thanks, AndyLitch. This works, but is not really speeding things up for me. I see that you are setting the application.calculation to xlmanual. Should that speed things up?

  4. #4
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: sort column based on number of duplicate values

    Yes, it should speed it up somewhat because i believe when you autofill, it keeps recalculating...

    You could also try Application.ScreenUpdating = false/true

  5. #5
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    Re: sort column based on number of duplicate values

    Thanks you AndyLitch. I have added the Application.ScreenUpdating in addition to the manual application.calculation. It still isn't quite instantaneous, but given the nature of problem, perhaps this is as fast as I should expect it to run. Thanks for the help!

  6. #6
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: sort column based on number of duplicate values

    The other thing which will speed it up a tad more is to swop the autofil for copy/paste

  7. #7
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2013
    Posts
    116

    Re: sort column based on number of duplicate values

    Awesome. Thanks again.

+ 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