How Do I Sort Alpha Numerically?

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have numbers in the format of CB1, CB2, CB11, CB23 etc etc. When I do a sort it will only sort like below

CB1
CB10
CB11
CB2

rather than

CB1
CB2
CB10 etc etc

How do I get round this please?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
A suggestion is to extract the numbers into another column (helper) then you can sort on the helper column. Formula used here is:

Sheet7

BC
2CB11
3CB22
4CB1010
5CB1111

<colgroup><col style="width: 30px; font-weight: bold;"><col style="width: 64px;"><col style="width: 64px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
C2{=1*MID(B2,MATCH(TRUE,ISNUMBER(1*MID(B2,ROW($1:$9),1)),0),COUNT(1*MID(B2,ROW($1:$9),1)))}
C3{=1*MID(B3,MATCH(TRUE,ISNUMBER(1*MID(B3,ROW($1:$9),1)),0),COUNT(1*MID(B3,ROW($1:$9),1)))}
C4{=1*MID(B4,MATCH(TRUE,ISNUMBER(1*MID(B4,ROW($1:$9),1)),0),COUNT(1*MID(B4,ROW($1:$9),1)))}
C5{=1*MID(B5,MATCH(TRUE,ISNUMBER(1*MID(B5,ROW($1:$9),1)),0),COUNT(1*MID(B5,ROW($1:$9),1)))}

<tbody>
</tbody>
Formula Array:
Produce enclosing
{ } by entering
formula with CTRL+SHIFT+ENTER!

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie Html" target="_blank"> Excel Jeanie HTML 4
 
Upvote 0
Hi

If you would post more details maybe there could be an easier solution.

For ex. let's say that you say that the alpha part is always 2 characters and that the number of digits that follow it is maximum 4. In that case for a code in A2 you could simply use in B2:

=0+MID(A2,3,4)
 
Upvote 0
Ok I will shortly, so the only way would be by using a formula?
 
Upvote 0
Ok I will shortly, so the only way would be by using a formula?

... or vba, which would be a complex solution to a simple problem. You add the auxiliary column, sort and delete the auxiliary column (or hide it) in just seconds.

As far as I know, sorting directly, with some excel sort option, is not possible.
 
Upvote 0
Perhaps this:-
Results in column "B"
Code:
[COLOR="Navy"]Sub[/COLOR] MG05Apr33
[COLOR="Navy"]Dim[/COLOR] Ray
[COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] j [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
Ray = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))


[COLOR="Navy"]For[/COLOR] i = 1 To UBound(Ray)
    [COLOR="Navy"]For[/COLOR] j = i To UBound(Ray)
        [COLOR="Navy"]If[/COLOR] Val(Mid(Ray(j, 1), 3)) < Val(Mid(Ray(i, 1), 3)) [COLOR="Navy"]Then[/COLOR]
            Temp = Ray(i, 1)
            Ray(i, 1) = Ray(j, 1)
            Ray(j, 1) = Temp
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] j
[COLOR="Navy"]Next[/COLOR] i
Range("B1").Resize(UBound(Ray)) = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
I tried that Mick but it didn't work. You can see the result below.


Excel 2007
ABC
1Before CodeAfter CodeShould Be
2C1C1C1
3C10C10C2
4C100C100C3
5C101C2C4
6C102C20C5
7C103C3C6
8C104C30C7
9C105C4C8
10C106C40C9
11C107C5C10
12C108C50C11
13C109C6C12
14C11C60C13
15C110C7C14
16C111C70C15
17C112C8C16
18C113C80C17
19C114C9C18
20C115C90C19
21C116C61C20
22C117C41C21
23C118C11C22
24C119C71C23
25C12C31C24
26C120C51C25
27C121C81C26
28C122C21C27
29C13C101C28
30C14C91C29
31C15C52C30
32C16C72C31
33C17C22C32
34C18C42C33
35C19C32C34
36C2C82C35
37C20C62C36
38C21C12C37
39C22C102C38
40C23C92C39
41C24C23C40
42C25C103C41
43C26C63C42
44C27C33C43
45C28C83C44
46C29C53C45
47C3C43C46
48C30C13C47
49C31C73C48
50C32C93C49
51C33C54C50
52C34C14C51
53C35C104C52
54C36C84C53
55C37C34C54
56C38C24C55
57C39C74C56
58C4C44C57
59C40C64C58
60C41C94C59
61C42C45C60
62C43C25C61
63C44C85C62
64C45C55C63
65C46C75C64
66C47C65C65
67C48C35C66
68C49C105C67
69C5C15C68
70C50C95C69
71C51C66C70
72C52C86C71
73C53C76C72
74C54C16C73
75C55C46C74
76C56C36C75
77C57C106C76
78C58C56C77
79C59C26C78
80C6C96C79
81C60C87C80
82C61C107C81
83C62C17C82
84C63C57C83
85C64C37C84
86C65C47C85
87C66C67C86
88C67C27C87
89C68C77C88
90C69C97C89
91C7C48C90
92C70C108C91
93C71C28C92
94C72C68C93
95C73C38C94
96C74C18C95
97C75C78C96
98C76C58C97
Sheet3
 
Upvote 0
In your initial thread you stated that the data for sorted was like:-CB1, CB2, CB11, CB23 etc etc
Now you've changed it to :-
C1, C2, C11, C23 etc etc
If there is only one letter at the start change the "3" to a "2" as below:-
Rich (BB code):
If Val(Mid(Ray(j, 1), 2)) < Val(Mid(Ray(i, 1), 2)) Then
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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