case matters when sorting strings??

Barq

New Member
Joined
Nov 16, 2005
Messages
18
Hi,
I have a general question:
I was sorting some strings using IF statements i.e. If string1 > string2 etc...
I noticed that the list that I end up with has puts "a" after "Z".

Why does this happen? Is there a way to make it so that the end result will be in the order of "A", "a", "B", "b" .. etc?

I can't convert the strings to all upper case or proper case because these are company names that sometime have unique ways of writing their names, e.g. eBay Inc, so I want to preserve that formatting.

Thanks!
 

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.
You don't need to convert the actual strings, just use UCase (or Lcase) to compare them.
Code:
If UCase(string1)>UCase(string2) Then
You could also place this at the top of the module.
Code:
Option Compare Text
 
Upvote 0
Hi Barq

In answer to your question of why it happens, it is because of the ASCII codes assigned to each character ('A' = 65, 'B' = 66, ...., 'a' = 97, 'b' = 98 and so on) so when Excel is sorting and it sees two characters of A ('A' and 'a') it will sort 'A' before 'a' as 65 is less than 97 (sort ascending).

Hope this helps!

Richard
 
Upvote 0

Forum statistics

Threads
1,224,265
Messages
6,177,533
Members
452,782
Latest member
ZCapitao

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