Numerical difference between 2 characters

smoothman

New Member
Joined
Jul 1, 2009
Messages
12
Hi all,

Was wondering if you could help me.
How do I write in a formula to get the numerical difference between two characters.

So difference between a and c is 2.
Difference between h and q is 9..

If the first character is after the second, for example X and B then I want to count till the end and back, so y z a b .. difference is 4

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
If "a" is in cell A1, and "c" is in cell B1, use this formula:
Excel Formula:
=CODE(B1)-CODE(A1)
 
Upvote 0
What version of Excel are you using?
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Many thanks. I'm using the latest version of Excel.

@Joe4

The =code() formula works if the first letter is smaller than the second. So if it's A and D then it correctly comes back as 3.

However it doesn't work the other way. So if it is the difference between D and B then I don't want it to say -2. I want it to go forward and loop back. So E F G H I J K L M N O P Q R S T U V W X Y Z A B = 24

Any ideas here?
 
Upvote 0
Deleted, this was not what OP needs
 
Upvote 0
How about
Excel Formula:
=IF(A2<B2,CODE(UPPER(B2))-CODE(UPPER(A2)),CODE(UPPER(B2))+26-CODE(UPPER(A2)))
 
Upvote 0
Try this:
Excel Formula:
=IF(CODE(B1)-CODE(A1)>0,CODE(B1)-CODE(A1),CODE(B1)-CODE(A1)+26)
If you have Excel 365, you can shorten it to:
Excel Formula:
=LET(x,CODE(B1)-CODE(A1),IF(x>0,x,x+26))
 
Upvote 0
Note: Might you have a mixture of comparing upper case value to lower case values, i.e. compare "a" to "M"?
How exactly do you want those to work?
 
Upvote 0
Glad we could help & thanks for the feedback.

Please don't forget to update your profile, as requested. ;)
 
Upvote 0

Forum statistics

Threads
1,215,373
Messages
6,124,553
Members
449,170
Latest member
Gkiller

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