+ Reply to Thread
Results 1 to 25 of 25

how to sort the data such that the end of the previous job is the start of the next job?

  1. #1
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Unhappy how to sort the data such that the end of the previous job is the start of the next job?

    In excel, we have 2 columns start time and end time per job. how to sort the data such that the end of the previous job to start time of next job is sorted in ascending order?
    Basically I am working with Truck Start Time of the Job and End Time of the Job. For better Route Planning/Scheduling I need to sort the data in the above mentioned manner.
    From To Pickup Time Delivery time
    A B 0200 0800
    C D 0700 0800
    0700 0900[INDENT]0700 1000[INDENT]0700 1000[INDENT]0800 1100[INDENT]0800 1100[INDENT]0800 1100[INDENT]0800 1800[INDENT]0800 1900[INDENT]0800 1900[INDENT]0800 2000[INDENT]0800 1830[INDENT]0800 1830[INDENT]0800 1830[INDENT]0900 1100[INDENT]0900 1200[INDENT]0900 1200[INDENT]0900 1300[INDENT]0900 1300[INDENT]1100 1400[INDENT]1100 1400[INDENT]1100 1400[INDENT]1100 1400[INDENT]1100 1400[INDENT]1100 1400[INDENT]1200 1600

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,976

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Attach a sample workbook (not image).

    Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate.

    Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  3. #3
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Hi John Topley,

    Thanks for your prompt response.

    As requested, attached the sample data for sorting.

    Before Sheet:

    I have Job No in column D, Sub Job No which indicates the No of Truck Trips in Column E, From and To in Column H and I.
    Estimated Pickup time in column J and Estimated Delivery Time in column K. The From and To Customers are classified into zones.

    After Sheet:

    As the end state, it is best to sort the end of 1st job equals or greater than the start of the second job timing and same zone.
    If Same Zone is not available, different zone is also acceptable but need to have at least 1 hr buffer time to travel.
    I want to use this logic for route scheduling for the future jobs.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    IN WEST- WEST zone

    S.No. 47 start time 6:30
    S.No. 2 start time 7:00

    Will 47 come earlier to 2.
    S. No. 47 shoud be 2nd row then S. No. 2 shoud be 2rd row
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  5. #5
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Hi kvsrinivasamurthy,

    Thanks for your response.
    yes you are right in the West-West Zone, S.No 47 should be on the 2nd row and S.No 2 should be on 3rd row.
    While doing manual simulation,I copied S.No 47 in two rows (Row 19th and Row 25th) by mistake.
    One should be removed and the other one need to be moved to the 2nd row.
    Thanks for the clarification.

  6. #6
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Try this.
    Copy the sheet Before.

    Right click on sheet tab -->Move or copy --> Create a copy.

    Now sort this sheet

    Data --> Sort

    Key1 From zone A-Z
    Key2 To zone A-Z
    Key3 Pick Up Time A-Z

  7. #7
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Hi Kvsrinivasamurthy,

    Thanks for the support.

    It worked but it did not reflect the exact after sheet.
    The timing matched but practically when it is a different zone, same timing for the next job will not be possible.
    Seems like cannot do it without a macro or a SQL logic in the backend

  8. #8
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Pl upload file showing in the sorted sheet what changes are required.

  9. #9
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Hi kvsrinivasamurthy,

    Please refer to the attached file tab before(2), after the sorting is performed as you had helped me to do so.

    Those cells highlighted in yellow would be the required outcome.

    Basically to Sort by Zone,Pickup timing
    same zone to group all those into 1 truck's full day's job using the job requests from the customer.
    If different zone (1st Job in east, next job available is in the West, at least to have 1 hour interval)
    Hope this helps.
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    No cells are highlighted in any sheet.

  11. #11
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    I re-save with Excel 97-2003 version.
    Attached the picture as well for the highlighted cells.
    Attached Images Attached Images
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Here is the code for Macro "TripSheet".
    Before (2) sheet is the output of Macro.

    Column J and K are in the text format .I have changed it into time format in Column N and O. Column P is to show trip number.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by kvsrinivasamurthy; 10-18-2017 at 02:20 AM.

  13. #13
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Thank you soo much.. Macro works

  14. #14
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Hi kvsrinivasamurthy,

    If I try the same for a larger data range. It seems to create individual Trips for each row.
    It works well with the 1 day basis.
    I have not done any macro coding before. I just know the basics of it.
    Please check if the macro logic needs to be amended for a larger data range.
    Thanks for the support.
    Attached Images Attached Images
    Attached Files Attached Files

  15. #15
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    do you want trips date wise.

  16. #16
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Hi
    Trip wise please

  17. #17
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Try the code.

    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Thank you very much. The code is working.

  19. #19
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Hi,

    Could you please explain the below queries?

    LRpage = Range("A" & Rows.Count).End(xlUp).Row (is used for selection of the first row till the end of the sheet?)
    Sheets("Before").Copy Before:=Sheets(1) (Creating the output in the tab before the actual sheet)

    Range("N1") = "Pickup Time1" (Creating Row Header with name Pickup Time1)
    Range("O1") = "Delivery time1 " (Creating Row Header with name Delivery time1)

    Range("N2:O" & LRpage).Formula = "=LEFT(J2,2)& "":""&RIGHT(J2,2)" (Changing text to time)

    ActiveSheet.Sort.SortFields.Clear (Adding the Sorting Option and Clearing the Sorting that is currently existing)
    ActiveSheet.Sort.SortFields.Add Key:=Range("B1"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortTextAsNumbers (Please explain this why is range B1 is used? I understood it is sorting by Ascending Order, the data that is sorted similar to Advanced Sort Option in the excel. Is the Sorting done for the column Pickup Date1?)
    ActiveSheet.Sort.SortFields.Add Key:=Range("N1"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortTextAsNumbers (Please explain this why is range N1 is used? Is the Sorting done for the Delivery time1 here?)

    With ActiveSheet.Sort
    .SetRange Range("A2:O" & LRpage)
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply ( With Function is used. But why Range ("A2:O" & LRPage), xlPinYin is a sorting method used for?)
    End With

    Range("A1:O1").Cut Range("R1") (Please explain the process that is performed here)

    X = 1 (Why X is declared to be 1, SR declared to be 2 and LR declared to be 1)
    SR = 2
    LR = 1

    Do While WorksheetFunction.CountA(Range("A2:A" & LRpage)) <> 0 (Do While Loop Function is used to perform the Trip Count until the Rows are not equal to null)

    TripNo = 0 (TripNo to be started with 0)
    LR = LR + WorksheetFunction.CountIf(Range("B" & SR & ":B" & LRpage), Range("B" & SR)) (Please explain the process involved in this code)

    Do While WorksheetFunction.CountA(Range("A" & SR & ":A" & LR)) <> 0 (Please explain the process involved in this code)
    StNo = 0 (What is StNo used for)
    TripNo = TripNo + 1 (If the Condition satisfies it adds to the Trip No)

    For T = 2 To LR (LR is the Variable to store the count of last row, For Loop is performed till the last row but why start with 2?)

    If Range("A" & T) <> "" Then (If Then Decision Loop is used, but why "A" & T) not equal to null

    If StNo = 0 Then
    X = X + 1 (Please explain the process involved in this code)
    Range("A" & T & ":O" & T).Cut Range("R" & X) (Please explain the process involved in this code)
    StNo = 1 (Please explain the process involved in this code)
    Range("AG" & X) = "Trip " & TripNo (Please explain the process involved in this code)
    Else

    If TimeValue(Range("N" & T).Value) >= TimeValue(Range("AF" & X).Value) + TimeValue("1:00:00") _ (Please explain the process involved in this code)
    And UCase(Range("L" & T).Value) = UCase(Range("AD" & X).Value) Then (Please explain the process involved in this code)
    X = X + 1 (Please explain the process involved in this code)
    Range("A" & T & ":O" & T).Cut Range("R" & X) (Please explain the process involved in this code)
    Range("AG" & X) = "Trip " & TripNo (Please explain the process involved in this code)
    End If

    End If

    End If

    Next T (Please explain the process involved in this code)

    Loop

    SR = LR + 1 (Please explain the process involved in this code)
    Loop

    Range("A1:Q1").EntireColumn.Delete (why is the A1:Q1 is deleted?)
    Columns("A:P").EntireColumn.AutoFit (Auto fitting of Columns for columns A to P)

    Application.ScreenUpdating = True (For Faster Macro execution and not displaying the process that is performed)

    End Sub

    Thanks in Advance.

  20. #20
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    This code takes more time to run. I. Will change the code and post after sometime.

  21. #21
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Noted with thanks

  22. #22
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Sorry for the delay.
    Try this code. I will clear the doubts.


    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Hi,

    No worries.
    Thank you very much.
    The code is much faster than the previous one.
    Please find attached my queries for the new code.
    In the future for the same excel if there is a need to add another sorting field how to do so?
    Attached Files Attached Files

  24. #24
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    Pl see attached file.
    Attached Files Attached Files

  25. #25
    Registered User
    Join Date
    10-16-2017
    Location
    Singapore
    MS-Off Ver
    2016
    Posts
    15

    Re: how to sort the data such that the end of the previous job is the start of the next jo

    got it.. Thanks for the clarification

+ 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. [SOLVED] Macro to Start New Sheet +Sort Past Data
    By Adam Schaefer in forum Excel General
    Replies: 1
    Last Post: 03-07-2015, 01:28 AM
  2. [SOLVED] Start date must be next workday and non-holiday after previous task end date
    By Verdant in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-04-2015, 04:21 PM
  3. [SOLVED] Sort depending of previous selection
    By luis6777 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 06-12-2014, 04:53 PM
  4. Replies: 4
    Last Post: 11-20-2012, 06:00 PM
  5. Force descending sort to Start with Numbers
    By The Smuffer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-04-2006, 04:24 PM
  6. Offset (start after last row of previous procedure)-Merging macros
    By Mslady in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-21-2005, 09:47 AM
  7. Replies: 1
    Last Post: 10-11-2005, 01:05 PM

Tags for this Thread

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