+ Reply to Thread
Results 1 to 6 of 6

Macro to sort variable rows of data by a column in multiple sections

Hybrid View

  1. #1
    Registered User
    Join Date
    06-10-2013
    Location
    Hampton, VA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to sort variable rows of data by a column in multiple sections

    I use Excel 2007, but this particular file I save in xls. format because I distribute it to others. If the code just works for 2007 that's fine. I can just save the file in an earlier format before I distribute it.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to sort variable rows of data by a column in multiple sections

    Hi mannuhs

    Sort syntax is different for Excel 2007. This syntax will work in either. You indicated to sort through Column O but your Data extends to Column R so that's what I used. If you truly want Column O change the rng references.
    Let me know of issues.

    Option Explicit
    
    Sub SortStuff()
        Dim LR As Long, piRow As Long, CERow As Long, VOSRow As Long
        Dim ws As Worksheet
        Dim rng As Range
        Set ws = Sheets("Sheet1")
        With ws
            LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            piRow = .Columns("A").Find(what:="P1 Level Calls", LookIn:=xlValues, lookat:=xlWhole).Row
            CERow = .Columns("A").Find(what:="Vendor CE Meet", LookIn:=xlValues, lookat:=xlWhole).Row
            VOSRow = .Columns("A").Find(what:="Vendor On Site", LookIn:=xlValues, lookat:=xlWhole).Row
            Set rng = .Range(.Cells(piRow + 1, 1), .Cells(CERow - 5, "R"))
    
            With rng
                If rng.Rows.Count > 1 Then
                    .Sort Key1:=.Cells(piRow + 1, "J"), Order1:=xlAscending, Header:=xlGuess, _
                            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                            DataOption1:=xlSortNormal
                End If
            End With
    
            Set rng = .Range(.Cells(CERow + 1, 1), .Cells(VOSRow - 1, "R"))
    
            With rng
                If rng.Rows.Count > 1 Then
                    .Sort Key1:=.Cells(CERow + 1, "J"), Order1:=xlAscending, Header:=xlGuess, _
                            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                            DataOption1:=xlSortNormal
                End If
            End With
    
            Set rng = .Range(.Cells(VOSRow + 1, 1), .Cells(LR, "R"))
    
            With rng
                If rng.Rows.Count > 1 Then
                    .Sort Key1:=.Cells(VOSRow + 1, "J"), Order1:=xlAscending, Header:=xlGuess, _
                            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                            DataOption1:=xlSortNormal
                End If
            End With
        End With
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    06-10-2013
    Location
    Hampton, VA
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Macro to sort variable rows of data by a column in multiple sections

    Wow that worked perfect! Thanks so much!

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to sort variable rows of data by a column in multiple sections

    You're welcome...glad I could help.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.
    Thanks.

+ 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