+ Reply to Thread
Results 1 to 3 of 3

Sorting a sheet via 2 columns by times in 2 columns with blank cells in certain cells

Hybrid View

  1. #1
    Registered User
    Join Date
    11-12-2007
    Posts
    38

    Sorting a sheet via 2 columns by times in 2 columns with blank cells in certain cells

    Have tried to sort this sheet example via columns J first ascending and then by columns F ascending with excels sort function but
    due to the blank cells in column F at the top and column J at the bottom though, I get quite a mixed up sheet.

    How would I go about sorting this sheet with the first priority being:

    If column F is blank and column J has a time in it, I'd want that data at the top of the sheet.
    Next criteria I'd want is if both columns F and J have times in them, I'd prefer both columns sorted in ascending order with column F being priority ascending followed by column J ascending.
    Lastly if column J has no times in cells, I'd like the data at the bottom of the sheet.

    Any ideas or even better a macro, it's beyond me..
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: Sorting a sheet via 2 columns by times in 2 columns with blank cells in certain cells

    This macro uses a helper column (K in your example data). It uses a formula to return the priority based on the contents of F and J

    Priority descending:
    1: F= empty, J = time
    2: F and J = time
    3: F = Time, J = empty
    4: F and J are empty

    Then it sorts based on the priority column and columns F and J. It then deletes the priority helper column after the sort.

    Sub Sort_Times()
        Dim Lr As Long, Lc As Long
        Lr = Cells.Find("*", , , , 1, 2).Row
        Lc = Cells.Find("*", , , , 2, 2).Column + 1
        
        Application.ScreenUpdating = False
        
        Cells(1, Lc).Resize(Lr).FormulaR1C1 = "=IF((RC[-5]=0)*(RC[-1]>0),1,IF((RC[-5]>0)*(RC[-1]>0),2,IF((RC[-5]>0)*(RC[-1]=0),3,4)))"
        
        Range("A1", Cells(Lr, Lc)).Sort Key1:=Cells(1, Lc), Order1:=xlAscending, _
                                        Key2:=Range("F1"), Order2:=xlAscending, _
                                        Key3:=Range("J1"), Order3:=xlAscending, _
            Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
            xlSortNormal
            
        Columns(Lc).Clear
        
        Application.ScreenUpdating = True
        
    End Sub

  3. #3
    Registered User
    Join Date
    11-12-2007
    Posts
    38

    Re: Sorting a sheet via 2 columns by times in 2 columns with blank cells in certain cells

    Gave it a try m8 but it sorts it with times and dates all over the place.Will try and post it this arvo to show you what it did to my data..

+ 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