Date and Time Not Sorting

zalik22

Board Regular
Joined
Dec 14, 2010
Messages
120
Office Version
  1. 365
Platform
  1. Windows
Hi,

I used to have time/date data coming in with m/d/yyyy h:mm AM/PM format and now it has a comma in the middle of it (m/d/yyyy, h:mm AM/PM) and after running my code, it is not sorting properly. Any idea what's wrong?

Code:
'   Find which column "Local Start Time" appears in
    Rows("1:1").Find(What:="Local Start Time", After:=Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    sortAdd = ActiveCell.Address(0, 0)
  
'   Convert entries in Date column to valid dates
    Columns(ActiveCell.Column).TextToColumns Destination:=Range(sortAdd), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 3), TrailingMinusNumbers:=True
          
'   Format columns
    Columns(ActiveCell.Column).NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"

'   Set sort range by using current region
    Range("A1").CurrentRegion.Sort _
        Key1:=Range(sortAdd), Order1:=xlAscending, Header:=xlYes
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
The comma makes it an unrecognised date format, do a find/replace to remove the comma
 
Upvote 0
The comma makes it an unrecognised date format, do a find/replace to remove the comma
I manually removed the comma before running the code and it didn't work. It sorts by date then 1pm rows instead of the 7am rows.
 
Upvote 0
Does this work?

old format:

5/9/2024 6:35 AM
5/10/2024 3:19 PM
5/8/2024 1:16 PM
5/7/2024 3:57 PM

new format:

9/9/2024, 8:49 PM
9/9/2024, 8:22 PM
9/9/2024, 7:19 PM
9/9/2024, 7:03 PM
 
Upvote 0
They are all PM times which doesn't show the issue you stated (not sure yet whether you putting them in a table will work for testing or not)
 
Upvote 0
When I run the sort code, with the old data format (no code), it sorted by date and time correctly (AM rows then PM).

The new format with the comma sorts off the date, then time (ignoring AM/PM) so 1pm rows are first, then 10am, then 11am, then 12pm, then 2pm, etc.
They are all PM times which doesn't show the issue you stated (not sure yet whether you putting them in a table will work for testing or not)
 
Upvote 0
Here is another snippet of code:

VBA Code:
'Sort by Local Start Time
Dim lastrow As Long

    With ActiveWorkbook.Worksheets("data")
    
    
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Sort.SortFields.Clear

        .Sort.SortFields.Add Key:=.Range("A1").Resize(lastrow), _
                             SortOn:=xlSortOnValues, _
                             Order:=xlAscending, _
                             DataOption:=xlSortNormal
        .Sort.SetRange .Range("A1:Z1").Resize(lastrow)
        .Sort.Header = xlYes
        .Sort.MatchCase = False
        .Sort.Orientation = xlTopToBottom
        .Sort.SortMethod = xlPinYin
        .Sort.Apply
    End With
'End Sort by Local Start Time
 
Upvote 0
Still need a sample of your data with the comma, before the sort and showing AM and PM (not re-typed and not an image, which is why I asked for you to use XL2BB), so we can test.

The code is pretty much irrelevant by what you have posted indicates it is your data layout that is the issue.
 
Upvote 0
A small edit to your original code
VBA Code:
'   Find which column "Local Start Time" appears in
    Rows("1:1").Find(What:="Local Start Time", After:=Range("A1"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    sortAdd = ActiveCell.Address(0, 0)

'   Gets rid of Commas in Active Column
 Columns(ActiveCell.Column).Replace ",",""

'   Convert entries in Date column to valid dates
    Columns(ActiveCell.Column).TextToColumns Destination:=Range(sortAdd), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 3), TrailingMinusNumbers:=True
 
'   Format columns
    Columns(ActiveCell.Column).NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"

'   Set sort range by using current region
    Range("A1").CurrentRegion.Sort _
        Key1:=Range(sortAdd), Order1:=xlAscending, Header:=xlYes
 
Upvote 0

Forum statistics

Threads
1,221,931
Messages
6,162,898
Members
451,801
Latest member
u551

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