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?
 
Sorry my mistake, its not for me its for a colleague. I think there may be a mixture of 1 letter and 2 letters, can the code be made for both possibilities please?
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this:-

Code:
[COLOR=Navy]Sub[/COLOR] MG05Apr53
[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] jj [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
[COLOR=Navy]Dim[/COLOR] ii [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
[COLOR=Navy]Dim[/COLOR] Temp [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
Ray = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR=Navy]For[/COLOR] i = 1 To UBound(Ray)
    [COLOR=Navy]For[/COLOR] j = i To UBound(Ray)
        jj = IIf(IsNumeric(Mid(Ray(j, 1), 2, 1)), 2, 3)
        ii = IIf(IsNumeric(Mid(Ray(i, 1), 2, 1)), 2, 3)
        [COLOR=Navy]If[/COLOR] Val(Mid(Ray(j, 1), jj)) < Val(Mid(Ray(i, 1), ii)) [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
With this the code does work but not quite as needed. Please see below. Its putting all the numbers in order but really it needs to put the letters then numbers like the reult in the 3rd column.


Excel 2007
ABC
1Before CodeAfter CodeShould Be
2C1C1C1
3C10CB1C2
4C11C2C3
5C12CB2C4
6C13C3C5
7C14CB3C6
8C15C4C7
9C16CB4C8
10C17C5C9
11C18CB5C10
12C19C6C11
13C2CB6C12
14C3C7C13
15C4CB7C14
16C5C8C15
17C6CB8C16
18C7C9C17
19C8CB9C18
20C9C10C19
21CB1CB10CB1
22CB10C11CB2
23CB11CB11CB3
24CB12C12CB4
25CB13CB12CB5
26CB14C13CB6
27CB15CB13CB7
28CB16C14CB8
29CB17CB14CB9
30CB18C15CB10
31CB19CB15CB11
32CB2C16CB12
33CB20CB16CB13
34CB3C17CB14
35CB4CB17CB15
36CB5C18CB16
37CB6CB18CB17
38CB7C19CB18
39CB8CB19CB19
40CB9CB20CB20
Sheet3
 
Upvote 0
Also Mick I've been informed that there will be other columns that will need sorting with the data, so maybe rather than putting the result in column b can the code sort column a and the respective data alongside also. Just like if you selected the entire sheet and used the normal sort function in Excel. Thanks.
 
Upvote 0
Try this:-
The number of columns sorted, relates to the length of columns with data in row 1
Code:
[COLOR="Navy"]Sub[/COLOR] MG06Apr30
[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] jj      [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] ii      [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Temp    [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Lst     [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Ac      [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Lst = Cells("1", Columns.Count).End(xlToLeft).Column
Ray = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp)).Resize(, Lst)


[COLOR="Navy"]For[/COLOR] i = 1 To UBound(Ray, 1)
    [COLOR="Navy"]For[/COLOR] j = i To UBound(Ray, 1)
        jj = IIf(IsNumeric(Mid(Ray(j, 1), 2, 1)), 2, 3)
        ii = IIf(IsNumeric(Mid(Ray(i, 1), 2, 1)), 2, 3)
         [COLOR="Navy"]If[/COLOR] Val(Mid(Ray(j, 1), jj)) < Val(Mid(Ray(i, 1), ii)) [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]For[/COLOR] Ac = 1 To Lst
                Temp = Ray(i, Ac)
                Ray(i, Ac) = Ray(j, Ac)
                Ray(j, Ac) = Temp
            [COLOR="Navy"]Next[/COLOR] Ac
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] j
[COLOR="Navy"]Next[/COLOR] i
Range("A1").Resize(UBound(Ray), Lst) = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks Mick I will try at work tomorrow.
 
Upvote 0
I just caught the part about sorting adjacent columns. This would just handle the column with the funky alphanumeric strings.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>  <br><SPAN style="color:#00007F">Sub</SPAN> example()<br><SPAN style="color:#00007F">Dim</SPAN> REX <SPAN style="color:#00007F">As</SPAN> RegExp<br><SPAN style="color:#00007F">Dim</SPAN> rexMC <SPAN style="color:#00007F">As</SPAN> MatchCollection<br><SPAN style="color:#00007F">Dim</SPAN> rngData <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> arrData <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> arrSplit <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> n <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> wks <SPAN style="color:#00007F">As</SPAN> Worksheet<br>  <br>  <SPAN style="color:#00007F">With</SPAN> Sheet1<br>    <SPAN style="color:#00007F">Set</SPAN> rngData = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>  <br>  <SPAN style="color:#007F00">'// Plunk values into array //</SPAN><br>  arrData = rngData.Value<br>  <br>  <SPAN style="color:#007F00">'// in case only one cell   //</SPAN><br>  <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> IsArray(arrData) <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>  <br>  <SPAN style="color:#007F00">'// Resize intermediary array //</SPAN><br>  <SPAN style="color:#00007F">ReDim</SPAN> arrSplit(1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(arrData), 1 <SPAN style="color:#00007F">To</SPAN> 2)<br>  <br>  <SPAN style="color:#00007F">Set</SPAN> REX = CreateObject("VBScript.RegExp")<br>  REX.Pattern = "([A-Za-z]+)([0-9]+)"<br>  <br>  <SPAN style="color:#00007F">For</SPAN> n = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(arrSplit)<br>    <SPAN style="color:#00007F">If</SPAN> REX.Test(arrData(n, 1)) <SPAN style="color:#00007F">Then</SPAN><br>      <SPAN style="color:#007F00">'// Rip letters and numbers seperate  //</SPAN><br>      <SPAN style="color:#00007F">Set</SPAN> rexMC = REX.Execute(arrData(n, 1))<br>      arrSplit(n, 1) = rexMC(0).SubMatches(0)<br>      arrSplit(n, 2) = rexMC(0).SubMatches(1)<br>    <SPAN style="color:#00007F">Else</SPAN><br>      MsgBox "Bad Val", 0&, vbNullString<br>      <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>  <SPAN style="color:#00007F">Next</SPAN><br>  <br>  <SPAN style="color:#00007F">Set</SPAN> wks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)<br>  <br>  <SPAN style="color:#007F00">'// Dump, sort, repopulate intermediary array //</SPAN><br>  <SPAN style="color:#00007F">With</SPAN> wks.Range("A1").Resize(UBound(arrSplit), 2)<br>    .Value = arrSplit<br>    .Sort .Cells(1), xlAscending, .Columns(2).Cells(1), , xlAscending, , , xlNo, , <SPAN style="color:#00007F">False</SPAN>, xlSortColumns, xlPinYin<br>    arrSplit = .Value<br>  <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>  <br>  <SPAN style="color:#007F00">'// Repopulate values array.  //</SPAN><br>  <SPAN style="color:#00007F">For</SPAN> n = 1 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(arrData)<br>    arrData(n, 1) = arrSplit(n, 1) & arrSplit(n, 2)<br>  <SPAN style="color:#00007F">Next</SPAN><br>  <br>  <SPAN style="color:#007F00">'// Dump back into original range //</SPAN><br>  rngData.Value = arrData<br>  <br>  <SPAN style="color:#007F00">'// Kill tmp WB //</SPAN><br>  wks.Parent.Saved = <SPAN style="color:#00007F">True</SPAN><br>  wks.Parent.Close <SPAN style="color:#00007F">False</SPAN><br>  <br>End <SPAN style="color:#00007F">Sub</SPAN></FONT>

Hope that helps,

Mark
 
Upvote 0
I tried that on a practice workbook and got a compile error saying user-defined type not defined and pointed to the variable REX As RegExp?
 
Upvote 0
Sorry, I wrote in early-binding. Set a reference to 'Microsoft VBScript Regular Expressions 5.5' or for late binding, change declarations to:

Dim REX As Object
Dim rexMC As Object

Mark
 
Upvote 0
Now I have a box with Bad Val in it? Don't go to too much trouble I am sure Micks will do the job, besides I will need a code to sort all columns not just the first column. Thanks GTO.
 
Upvote 0

Forum statistics

Threads
1,215,443
Messages
6,124,890
Members
449,194
Latest member
JayEggleton

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