Results 1 to 1 of 1

Sort Time Values Chronologically...Tricky format!

Threaded View

  1. #1
    Forum Contributor
    Join Date
    07-12-2013
    Location
    Los angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    109

    Sort Time Values Chronologically...Tricky format!

    As a follow-up to my previous thread, I now need to sort a column of times into chronological order using my VBA code. I've now converted from a simple string of text to a time format, but am having issues sorting into proper order.

    For background info, this is a timetable for bus routes. I need to combine the eastbound and westbound times in column H in chronological order. In the column of times (column H), "a" stands for AM, "p" stands for PM, and "x" stands for end-of-day (meaning if a bus route's last time is at 12:30a, it is considered "x"). I have attached my worksheet (which I manually sorted column H chronologically in the desired order for illustration purposes), as well as my current VBA code. Please let me know if I can provide any additional information.

    Thanks!

    Option Explicit
    
    Sub colon()
    
    Dim lastcolumn As Integer
    Dim lastrow As Integer
    Dim cell As Range
    Dim n As Long
    Dim timeWithSpace As String
    
    'Find last column of data
    With ActiveWorkbook.ActiveSheet.Range("A1")
        lastcolumn = Cells(2, Columns.Count).End(xlToLeft).Column
        lastrow = Cells(Rows.Count, lastcolumn).End(xlUp).Row
    
        Range(.Offset(1, lastcolumn - 2), .Offset(lastrow, lastcolumn - 2)).Name = "combined"
        
    End With
    
    
    'Convert simple time into formatted time
    For Each cell In Range("combined")
    
        n = Len(Trim(cell))
        
        
        Debug.Print timeWithSpace
                 
        If n = 4 Or n = 5 Then
            cell = Left(Trim(cell), n - 3) & ":" & Right(Trim(cell), 3)
            cell.NumberFormat = "h:mm AM/PM"
        End If
    Next
               
        
    End Sub
    121612 Weekday El Monte Bus Trips.xlsm
    Last edited by aloha31; 07-15-2013 at 02:18 PM.

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