Unexpected behavior in Excel when sorting

9 replies · 3K views · last reply
DaveC426913
Gold Member
2025 Award
Messages
24,779
Reaction score
9,206
TL;DR
When I try to sort a spreadsheet, it ignores the first row (not a header.)
I'm trying to sort this sheet in Excel. I want to sort it on Column D.

Excel window titled Missing Bills.xlsx on the Data tab, with column D header circled in red; sheet rows show dates in column E and text notes in column F.


I select the entire page by clicking the black arrow in the upper left corner:

Screenshot of Excel with the whole sheet selected and the first data row, A1 containing 33500, shown unselected; the Data ribbon tab is open.


Now I Data > Sort which brings up a dialogue where I specify the column I want to sort by :

Excel Sort dialog over a spreadsheet, sorting by Column D in A to Z order with My data has headers checked; the first data row appears unselected.


(I can already tell there may be a problem - the first row has become deselected. This was not my doing. It happens every time):

Excel window titled Missing Bills.xlsx with the Sort dialog open, Sort by Column D, Values, A to Z; row 1 is outlined in red and the rest of the sheet is selected.


An indeed, the sheet is sorted on column D, except the first row:

Screenshot of Excel with 'Missing Bills.xlsx' open showing the Data ribbon; cell B4 is selected with 'TSEGAY GUCHU' in the formula bar. Red annotations circle 'Holz' in row 1 of column D and point to 'Holz' in row 10.


It doesn't matter how many times I do this - it's not a slip of the thumb - it always happens on this sheet. (And I have encountered this before, so it's not an artefact of this sheet only).

It's not a matter of data type of something life that. It happens on any other column I try to sort on:This is sorting on column A:

Excel screenshot of the Missing Bills.xlsx sheet with cell A1 selected showing 33500, a red arrow pointing to it, and columns of dates, names and notes below.


This is sorting on column F:

Excel spreadsheet titled Missing Bills.xlsx with a red arrow pointing to column F; row 1 is selected and the first data row is not sorted with the rest of the list.


The only thing that worked was to cut and paste the row from the top to the bottom:

Excel spreadsheet titled Missing Bills.xlsx showing rows of patient records with red arrows pointing to the header row 1 and to row 25, which is highlighted as selected.


And now it sorts OK:

Excel spreadsheet titled Missing Bills.xlsx with a red arrow pointing to a cell in column D; rows show numeric IDs, names, dates in Sep-23 and Aug-23, and short notes, with the first data row highlighted.
What am I doing wrong?
 
[ UPDATE ]
Man there is nothing so effective for trouble-shooting as writing up the problem unambiguously in preparation for asking for help... :sorry:

When I went to update the veeery last screenshot, I saw this:
Excel Sort dialog box with the 'My data has headers' checkbox circled in red, and the Sort by column set to a column containing OV cs neg text, Values, A to Z.
 
  • Like
  • Haha
Likes   Reactions: jrmichler, berkeman, jedishrfu and 4 others
jedishrfu said:
Is that why your first post has all strikeout text?
Yes. Save readers the time of reading through it all, only to discover I'd found the answer.

And I figured, since I'd already made the entire post, deleting it would only deprive some future readers in the same boat from reaping the benefit of my trials and tribulations.
 
  • Like
Likes   Reactions: jedishrfu, Baluncore and berkeman
DaveC426913 said:
Man there is nothing so effective for trouble-shooting as writing up the problem unambiguously in preparation for asking for help...
Also for solving engineering problems....
 
Probably the most important advantage of a forum like PF, is that the OP needs to work out what question to ask. After reading threads like this, we all become humbler.

“The only interesting answers are those which destroy the question”. —Susan Sontag
 
Dave, I also had to track this down one time. Obvious once you know about the option but MS Office software has SO many options that it's hard to keep track of them all.
 
phinds said:
Dave, I also had to track this down one time. Obvious once you know about the option but MS Office software has SO many options that it's hard to keep track of them all.
Indeed. The gotcha in this case was that it the first row's behavior hinted at being a header, but the only way to define a row to behave as a header is to "freeze" it. Which of course, I did not do.

Knowing that's how Excel "handles" headers, one would not think to go looking for an option on a dialogue that said, effectively "Don't become a header, just pretend to act like one - for the moment - and then go back to being a regular row, like nothing ever happened." :mad:
 
  • Informative
  • Like
Likes   Reactions: phinds and Bystander
DaveC426913 said:
[ UPDATE ]
Man there is nothing so effective for trouble-shooting as writing up the problem unambiguously in preparation for asking for help... :sorry:

When I went to update the veeery last screenshot, I saw this:View attachment 334595
I wrote this in my form code:
'TARGET WITHIN RANGE - RUN JOB
ActiveCell.Select
Selection.Sort Key1:=ActiveCell, Order1:=xlAscending, _
HEADER:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
GoTo FINISHED
 
Gifovaco said:
I wrote this in my form code:
'TARGET WITHIN RANGE - RUN JOB
ActiveCell.Select
Selection.Sort Key1:=ActiveCell, Order1:=xlAscending, _
HEADER:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
GoTo FINISHED
Excel loves to think for you. In order to totally control it I run a script recursively that deletes ALL formatting. THEN I wrie what I want.... Excel is a 'trick' monster