+ Reply to Thread
Results 1 to 7 of 7

Sort Problem

Hybrid View

  1. #1
    Registered User
    Join Date
    08-28-2012
    Location
    Jakarta
    MS-Off Ver
    Excel 2007
    Posts
    22

    Sort Problem

    Hi,

    I have problem when sorting.
    I want to sorting by column AA, then I just need the top 5 data.
    How I do it with VBA?
    You can see data and result in the attachment.

    Thanx for helping me.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    01-29-2010
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel XP, Excel 2003, Excel 2007
    Posts
    27

    Re: Sort Problem

    Hi,

    You can record a macro with the built-in macro recorder for these actions. Then open the macro and refine it to suit your needs.

    Boris

  3. #3
    Forum Contributor
    Join Date
    11-12-2013
    Location
    Delhi
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Sort Problem

    pls check the range as I took only from you sheet...

    many thanks
    Rishi Saw

  4. #4
    Forum Contributor
    Join Date
    11-12-2013
    Location
    Delhi
    MS-Off Ver
    Excel 2016
    Posts
    135

    Re: Sort Problem

    ActiveWorkbook.Worksheets("DATA").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("DATA").Sort.SortFields.Add Key:=Range("AA2:AA23") _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("DATA").Sort
            .SetRange Range("A1:AA23")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("B7").Select
        Range(Selection, Selection.End(xlDown)).Select
        Rows("7:23").Select
        Range("B7").Activate
        Selection.Borders(xlLeft).LineStyle = xlNone
        Selection.Borders(xlRight).LineStyle = xlNone
        Selection.Borders(xlTop).LineStyle = xlNone
        Selection.Borders(xlBottom).LineStyle = xlNone
        Selection.Delete Shift:=xlUp
        Range("A8").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range("A8:AA20").Select
        ActiveCell.FormulaR1C1 = "s"
        ActiveWorkbook.Worksheets("DATA").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("DATA").Sort.SortFields.Add Key:=Range("AA9:AA20") _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("DATA").Sort
            .SetRange Range("A8:AA20")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("A14:AA14").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range("A14:AA20").Select
        Selection.Delete Shift:=xlUp
        Range("A13").Select
    End Sub

  5. #5
    Registered User
    Join Date
    08-28-2012
    Location
    Jakarta
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Sort Problem

    If the range always change (data always change every month), that means i must change the macro every time i used that?

    Thanx

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

    Re: Sort Problem

    Hi rainbowzzzzz,
    try it
    Sub ertert()
    Dim r As Range, i&
    For Each r In ActiveSheet.UsedRange.Columns(27).SpecialCells(2).Areas    ' column AA
        With r.CurrentRegion
            .Offset(, 1).Resize(, .Columns.Count - 1).Sort Key1:=.Cells(1, 27), Order1:=xlDescending, Header:=xlYes
            i = .Rows.Count - 6
            If i > 0 Then .Offset(6).Resize(.Rows.Count - 6).EntireRow.Delete
        End With
    Next r
    End Sub

  7. #7
    Registered User
    Join Date
    08-28-2012
    Location
    Jakarta
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Sort Problem

    Hi Nilem,

    It's work perfectly.

    Thanx for ur help.

+ 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] Problem with sort through VBA
    By Bob Berg in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-21-2012, 01:40 PM
  2. sort problem
    By Shira in forum Excel General
    Replies: 1
    Last Post: 02-12-2008, 06:23 PM
  3. Sort Problem
    By sposdog in forum Excel General
    Replies: 2
    Last Post: 08-23-2007, 03:43 PM
  4. Sort Problem
    By andyp161 in forum Excel Formulas & Functions
    Replies: 27
    Last Post: 09-07-2005, 12:05 AM
  5. sort problem with first row
    By smokiibear@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-15-2005, 11:05 AM

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