+ Reply to Thread
Results 1 to 2 of 2

Advace Sorting Based On Values

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2006
    Posts
    17

    Advace Sorting Based On Values

    Hi

    I get workbooks with three kinds of data in a certain column in every workbook. The data is positive, neutral and negative, i.e., 20, 0, -20, respectively. The values are always in the B column.

    I was wondering if a macro can do the following:

    1 Sort positive data (highest to lowest) (anything greater than zero)
    2 Delete rows of neutral data (0)
    3 Sort negative data (lowest to highest) (i.e, -20, -18, -12, -7, etc)


    Briefly speaking, sort positive values highest to lowest and negative values lowest to highest in the same column and delete rows with 0 values.


    Looking for some help
    Last edited by Maxal; 02-09-2015 at 09:22 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Advace Sorting Based On Values

    Hello Maxal,

    This macro will sort and delete the rows on the Activesheet of the ActiveWorkbook.

    Sub SortingMacro()
    
        Dim n       As Long
        Dim r       As Long
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = ActiveSheet
            
            Set Rng = Wks.UsedRange.Columns("B:B").Cells
            
                With Wks.Sort
                    .SortFields.Clear
                    .SortFields.Add Key:=Rng, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
                    
                    .Header = xlGuess
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SetRange Rng
                    .Apply
                End With
                    
            Set Cell = Rng.Find(0, , xlValues, xlWhole, xlByRows, xlNext, False, False, False)
            
            If Not Cell Is Nothing Then
                While Cell.Offset(n, 0) = 0: n = n + 1: Wend
                Cell.Resize(n, 1).EntireRow.Delete
                n = 0
            End If
            
            For r = Rng.Rows.Count To 1 Step -1
                If Rng.Cells(r, 1) < 0 Then n = n + 1
            Next r
            
            If n > 0 Then
                Set Rng = Wks.Range(Rng.Cells(n + 1, 1), Rng.Cells(Rng.Rows.Count, 1))
            End If
            
                With Wks.Sort
                    .SortFields.Clear
                    .SortFields.Add Key:=Rng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                    
                    .Header = xlNo
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SetRange Rng
                    .Apply
                End With
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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. Sorting based on one column values and removing duplicates!
    By devpp in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-26-2013, 09:41 AM
  2. [SOLVED] formula for advace filter
    By khurramfarooqpk in forum Excel General
    Replies: 1
    Last Post: 05-07-2013, 06:22 AM
  3. Sorting rows based on values in columns
    By midoindeedo in forum Excel General
    Replies: 1
    Last Post: 08-01-2012, 11:05 AM
  4. PowerPivot: Sorting based on a Values column
    By teh bunneh in forum Excel General
    Replies: 0
    Last Post: 06-14-2011, 10:58 AM
  5. [SOLVED] Sorting cell data based on values
    By veronica in forum Excel General
    Replies: 1
    Last Post: 01-25-2006, 04:30 PM

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