Enhancing Sort of Times To Distinuguish Between Before and After Times

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am at a point in my project that has me stumped again.

Consider this worksheet

Excel 2010
ABCDEFGHIJKLMNOPQRST
10Record IDDispatchRentalLocationActivityGroomPrepareSignatureLights OnLights Off1234Close
11
12
1342884043>1:45P787915:15P8:15PCWPWPLWPLWPLDR>1:45:00 PM
1442884041779096:00P8:00PCWPWPLWPLWPLDR>1:45:00 PM
1542884046<2:00P784023:00P4:30PCWPCUECUECUEDR<2:00:00 PM
1642884001<2:30P778965:15P8:15PCWPHPLHPLHPLDR<2:30:00 PM
1742884010779115:15P8:15PCWPHPLHPLHPLDR<2:30:00 PM
1842884014779265:15P8:15PCWPCULCULCULDR<2:30:00 PM
1942884002779335:45P8:45PCWPWPLWPLDR<2:30:00 PM
2042884003779335:45P8:45PCWPWPLWPLDR<2:30:00 PM
2142884007778766:00P8:15PCWPHPLHPLNRNRDR<2:30:00 PM
2242884009778728:30P10:30PCWPHPLHPLHPLCULCULDR<2:30:00 PM
2342884039>2:45P780706:15P11:00PCWPCULCULCULCULCULDR>2:45:00 PM
24
CWP


Right now, the data is sorted based on ascending order of times in column T. This is producing the proper results, but, not when you include the qualifiers in column S. What I need to do is put any > (after) times following any < (before) times. This database serves as a list of activities that need to be done in a chronological order. A final product would look like this:


Excel 2010
ABCDEFGHIJKLMNOPQRST
10Record IDDispatchRentalLocationActivityGroomPrepareSignatureLights OnLights Off1234Close
11
12
1342884046<2:00P784023:00P4:30PCWPCUECUECUEDR<2:00:00 PM
1442884001<2:30P778965:15P8:15PCWPHPLHPLHPLDR<2:30:00 PM
1542884010779115:15P8:15PCWPHPLHPLHPLDR<2:30:00 PM
1642884014779265:15P8:15PCWPCULCULCULDR<2:30:00 PM
1742884002779335:45P8:45PCWPWPLWPLDR<2:30:00 PM
1842884003779335:45P8:45PCWPWPLWPLDR<2:30:00 PM
1942884007778766:00P8:15PCWPHPLHPLNRNRDR<2:30:00 PM
2042884009778728:30P10:30PCWPHPLHPLHPLCULCULDR<2:30:00 PM
2142884043>1:45P787915:15P8:15PCWPWPLWPLWPLDR>1:45:00 PM
2242884041779096:00P8:00PCWPWPLWPLWPLDR>1:45:00 PM
2342884039>2:45P780706:15P11:00PCWPCULCULCULCULCULDR>2:45:00 PM
24
CWP


Here is the code that I am using currently to sort this range:
Code:
                    Dim lRowst As Long
                    Dim lRowed As Long
                    Dim vg As String
                    Dim cntdr As Long
                    Dim pp, bm As Long
                    Dim po As Long
                    Dim kl2 As String
                    Dim oRangeSort As Range
                
                    arr2 = Array("DT", "DTS", "DR", "FR", "FT", "CR", "CT", "GS") 'sort order
                    llastrow = .Range("R" & Rows.count).End(xlUp).row
                    Set rdata = .Range("R13:R" & llastrow)
    
                    For po = 0 To UBound(arr2)
                        vg = arr2(po) 'sort value
    
                        cntdr = Application.CountIf(rdata, vg) 'count of sort value
    
                        If cntdr > 0 Then 'there is no vg rows
            
                            On Error Resume Next
                            lRowst = Application.Match(vg, rdata, 0)
                            On Error GoTo 0
            
                            lRowst = lRowst + 12            'start of sort value range
                            lRowed = lRowst + cntdr - 1     'end of sort value range
                
                          'determine sort times
                            For pp = lRowst To lRowed
                                'tournament services
                                If .Range("R" & pp) = "DTS" Then
                                    kl2 = InStr(.Range("B" & pp).Value, "-") - 1
                                    .Range("T" & pp).Value = TimeValue(Mid(.Range("B" & pp).Value, 8, InStr(.Range("B" & pp).Value, "-") - 1 - 7))
                                Else 'regualr diamond
                                    bm = Len(.Range("B" & pp)) - 1 'length of dispatch value
                                    If bm > 0 Then 'setup
                                        If bm > 12 Then
                                            .Range("T" & pp).Value = TimeValue(Right(.Range("B" & pp).Value, bm - 8))
                                        Else
                                            .Range("S" & pp).Value = Left(.Range("B" & pp).Value, 1)
                                            .Range("T" & pp).Value = TimeValue(Right(.Range("B" & pp).Value, bm))
                                        End If
                                    Else 'close
                                        If vg = "DR" Then
                                            bm = Len(.Range("Q" & pp))
                                            '.Range("S" & pp).Value = Left(.Range("Q" & pp).Value, 1)
                                            .Range("T" & pp).Value = TimeValue(Right(.Range("Q" & pp).Value, bm))
                                        End If
                                    End If
                                End If
                            Next pp
                    
                            Set oRangeSort = .Range("A" & lRowst & ":T" & lRowed)
                            oRangeSort.Sort key1:=Range("T" & lRowst), order1:=xlAscending, key2:=Range("R" & lRowst), order2:=xlDescending, Header:=xlNo, _
                                OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
                                Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
                        Else
                            'MsgBox vg & "No"
                        End If
                    Next po
                End If

I hope I've provided enough information and clear enough explanation of what I wish to accomplish. I urge anyone that wishes to provide some direction to ask for clarification if needed in an effort to help me.

I appreciate everyone's contribution.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi, I don't know how to write VBA (though I can read it), so I normally solve many of my excel problems with formulae, or macros.

Are you prepared to add a basic formula into V13 and copy down ... =IF(T13="<",1,2)

Then add a Command Button, that you press any time you want the data re-sorted using the following macro code ... (simply extend the ranges in the code if you have more rows to play with).
Code:
Sub sort_times()
'
' sort_times Macro
'


'
    Range("B13:V23").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("V13:V23") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("U13:U23") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B13:V23")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
Hi Palaeontolgy ...

I see where you are going with this and the concept should do what I need to do. I just needed someone with fresh thinking to see a relatively simple solution.
I think I'll be able to adapt my code to integrate your suggestion.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top