Why does this sort not work please!

S.H.A.D.O.

Well-known Member
Joined
Sep 6, 2005
Messages
1,915
Good afternoon,

I have the code...

Code:
    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range("B56:B69"), Order:=xlAscending
    End With
    With ActiveSheet.Sort
        .SetRange Range("B56:M69")
        .Apply
    End With
    Range("O2").Select

The numbers in column B are 1-14. When the code above is run, it puts 14 at the top and then the numbers 1-13 below the 14.

Thanks in advance.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
It sounds like it is treating your first row as a header row. You can explicitly tell it to do that (or not), like this:
Code:
    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range("B56:B69"), Order:=xlAscending
    End With
    With ActiveSheet.Sort
        .SetRange Range("B56:M69")
[COLOR=#ff0000]        .Header = xlNo[/COLOR]
        .Apply
    End With
    Range("O2").Select
 
Upvote 0
Thanks for the reply Joe4, unfortunately that doesn't work. I have the numbers 1-14 custom formatted as 00.When I remove this formatting it works OK!
 
Last edited:
Upvote 0
Not to worry, I have sorted it out!

It was the fact that the numbers 1-13 were some how formatted slightly different to the number 14 [although retained the 00 formatting!]. I just copied the number 14 down and then manually input the numbers 1-13. I then ran the Macro and it worked.

Thanks for your help Joe4, it is appreciated.
 
Last edited:
Upvote 0
I have a strong suspicion I know what the problem was. Custom formatting only applies to numbers.
I am guessing that some of your entries were entered as numbers, and some with entered as text. Those will sort differently (numbers are typically sorted before text).
 
Upvote 0
Hi Joe4,

I have a strong suspicion I know what the problem was. Custom formatting only applies to numbers.
I am guessing that some of your entries were entered as numbers, and some with entered as text. Those will sort differently (numbers are typically sorted before text).

I am absolutely sure that's what it was.

Thanks again, it is appreciated.
 
Upvote 0
You are welcome.
Glad you have it working!
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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