Results 1 to 6 of 6

VBA to 2-step sort multiple columns

Threaded View

  1. #1
    Registered User
    Join Date
    08-12-2014
    Location
    USA
    MS-Off Ver
    365
    Posts
    8

    VBA to 2-step sort multiple columns

    Link to cross-post:
    http://chandoo.org/forum/threads/vba...7/#post-203641
    https://www.mrexcel.com/forum/excel-...ml#post4814278

    I have an Excel spreadsheet that I need to sort 3 columns and varying rows (see below). What I would like is VBA code to sort all columns and rows through item 'P' by Quantity (largest to smallest). Then I need to sort all 3 columns a second time by Sort Order (smallest to largest) neglecting the items with zero quantities. The number of items will always be the same so the first sort range will always be the same.

    Here is a link to a spreadsheet similar to the one I will be using: https://app.box.com/s/zxvg9acycxrqv067vfc8kct4rroww6qp

    I tried to record a macro for this but it only works for this specific example (Items may not always have quantities and/or the quantities will change value so the second sort range (rows) will change). Here is what the macro recorded:

    Sub Sort1()
    '
    ' Sort1 Macro
    '
    '
        Range("A4:C20").Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A5:A20") _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A4:C20")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("A4:C16").Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C5:C16") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A4:C16")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Can someone provide help? Thanks!
    Last edited by jebersohl; 04-30-2017 at 10:31 AM. Reason: Cross-post update & sample excel doc provided

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Using a VBA to sort Multiple Columns
    By BT3 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-07-2016, 03:30 PM
  2. [SOLVED] Sort by multiple columns with sort criteria cell dependent
    By BRISBANEBOB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-24-2014, 02:22 AM
  3. 2-step transpose and sort by unique id
    By stp79 in forum Excel General
    Replies: 2
    Last Post: 08-13-2012, 10:59 PM
  4. Assign number based on sort order (Step Ladder points)
    By benjya in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-21-2012, 01:24 AM
  5. Sort multiple columns with multiple formulas without returning #R
    By bellsjrb in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-14-2006, 05:05 AM
  6. [SOLVED] Sort multiple columns with multiple formulas without returning #R
    By bellsjrb in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-14-2006, 05:00 AM
  7. Replies: 2
    Last Post: 07-07-2006, 10:15 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