Trouble with sorting cells that are dates (and custom as " d:m:h:s ")... & the sort doesn't always get it right...(?)

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
edit: sorry, but I typed the title incorrect. It wont allow me to edit that either. It should say: " d:h:m:s "

numbers that I have formatted so they appear this way:
Capture405.JPG

like this:
Capture407.JPG

do not sort in the correct order when I use:
VBA Code:
ActiveWorkbook.Worksheets("NOT-ACTIVE").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("NOT-ACTIVE").Sort.SortFields.Add Key:=Range("N15", Range("N15").End(xlToRight)), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

And it doesnt make any difference how I format the cells, custom (I tried many different ways), general, time, date, text... it always comes out the same.
Capture408.JPG


Any suggestions for how can I change the format or number sequence in order to make it sort correctly?


When sorted from left-to-right, it looks like this: (all is in the correct order until the cells that are circled in red)
Capture403.JPG
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi

I feel the issue is with the way the format is

With the formula INT(N6-N18) & ":"& TEXT(N6-N18, "h:m:s"), a string is formed
In this string (it is not a number), 6>1 implying 6>15

To avoid this, please do a straight subtraction N6-N18 (no INT....&TEXT), then go to cell formatting (Cntl +1), select Custom-->General-->
edit 'General' and type dd hh:mm:ss
This should do the sorting correctly

Please check and let know whether this works
 
Upvote 1
Solution
Hi

I feel the issue is with the way the format is

With the formula INT(N6-N18) & ":"& TEXT(N6-N18, "h:m:s"), a string is formed
In this string (it is not a number), 6>1 implying 6>15

To avoid this, please do a straight subtraction N6-N18 (no INT....&TEXT), then go to cell formatting (Cntl +1), select Custom-->General-->
edit 'General' and type dd hh:mm:ss
This should do the sorting correctly

Please check and let know whether this works
Thank you for the prompt response.

and... that did it!!!

I didnt think that it was going to make a difference with how it was going to sort (but I was loving how much simpler it was!), but sure enough, you were right and it appears to have sorted it out correctly!

(I say 'appears' because I still haven't looked at all the data yet real close, but I think it def did work.) Thanks again!
 

Attachments

  • Capture409.JPG
    Capture409.JPG
    106.7 KB · Views: 2
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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