Date and Time Not Sorting

zalik22

Board Regular
Joined
Dec 14, 2010
Messages
122
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
 
A small edit to your original code
You mean what I suggested in post 2, did you test with what the OP posted in post 5 (which was originally in table tags within code tags), it also needs a space and a unicode character removed?
 
Last edited:
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
You mean what I suggested in post 2, did you test with what the OP posted in post 5 (which was originally in table tags within code tags), it also needs a space and a unicode character removed?
Removing the comma did not work, I cannot install the xl2bb add on, might be limited permissions on my laptop.
 
Upvote 0
It won't work if what you posted in post 5 is correct (not sure if your method of displaying is giving the correct info).
Try selecting the column and run the below and see if it changes the data

VBA Code:
Sub Fixit()
    With Selection
        .Replace ",", "", xlPart
        .Replace ChrW(8239) & "PM", " PM", xlPart
    End With
End Sub
 
Upvote 0
It won't work if what you posted in post 5 is correct (not sure if your method of displaying is giving the correct info).
Try selecting the column and run the below and see if it changes the data

VBA Code:
Sub Fixit()
    With Selection
        .Replace ",", "", xlPart
        .Replace ChrW(8239) & "PM", " PM", xlPart
    End With
End Sub
This removed the PM and sorted them on top. The AM dates and times were sorted below the dates with no AM/PM. The time also changed to 13:00 for 1:00PM.
 
Upvote 0
The PM will either be there or not depending on how your cells are formatted (that is down to you to deal with)
The AM dates and times were sorted below the dates with no AM/PM
I never put anything for the AM times as the code was to test if it corrected the data you posted in the thread , which didn't have the AM (by the way nothing should have sorted if you done as I instructed and ran the code as I posted it, you have obviously added it to your code before testing... just for reference this is not a good idea when someone asks you to test something, as it complicates the results).

You need to add a separate line to cover the AM cells as per the below and you need to add code for the formatting if you require it, as they are now real dates and times that default to your default format and not text.

Rich (BB code):
Sub Fixit()
    With Selection
        .Replace ",", "", xlPart
        .Replace ChrW(8239) & "PM", " PM", xlPart
        .Replace ChrW(8239) & "AM", " AM", xlPart
    End With
End Sub
 
Last edited:
Upvote 1
Solution
Please note I have edited the previous post as I hadn't changed the 2nd PM to AM
 
Upvote 0
Hi, I caught it. Seems to sort, I just need to work on getting the AM and PM to show up now. It's in the cell but not being displayed.
Please note I have edited the previous post as I hadn't changed the 2nd PM to AM
 
Upvote 0
It isn't in the cell. It needs formatting the same as you would any other date cell, just turn on the macro recorder and format it manually and you'll get the code.
 
Upvote 0
It isn't in the cell. It needs formatting the same as you would any other date cell, just turn on the macro recorder and format it manually and you'll get the code.
Done, thanks for your help and patience with this! I marked the solution too.
 
Upvote 0
You're welcome, happy you got it "sorted"

P.S. as one of the other MVP's pointed out to me you can probably get away with the just the line below to cover both the AM and PM

VBA Code:
.Replace ChrW(8239), " ", xlPart

Edited because I still can't tell the difference between your and you're
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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