Sort numbers with letters at the end

rjplante

Well-known Member
Joined
Oct 31, 2008
Messages
569
Office Version
  1. 365
Platform
  1. Windows
I have a column of data listed below. I want to be able to sort the numbers first and then the numbers based on the letters.

Original Data
907
1213
1508
907A
1215
1305
1508A
1691
1710
1508B
1711
1306

Sorted Data (ideal)
907
907A
1213
1215
1305
1306
1508
1508A
1508B
1691
1710
1711

I have gone into the advanced options and added a custom list that reads from A-Z, but that did not help in the sorting. How do I get this to sort correctly, using the numbers and then the letters (if they are present) to build the complete correctly sorted list? From other sources I have read I may need a helper cell, or reference column to manage the parsing of the data. I will be creating a macro which generates a user form and then adds the new entered number to the list column and then sorts the column which would place the number in its correct position.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Assumptions - you have excel365. The letters are always just one letter on the end of what would otherwise be a number. There is room for helper columns.

MrExcelPlayground2.xlsm
PQRST
19079079071907
21213121312133907A
315081215121541213
4907A1305130551215
512151306130661305
613051508150871306
71508A16911691101508
8169117101710111508A
9171017111711121508B
101508B1508A150871691
1117111508B150871710
121306907A90711711
Sheet35
Cell Formulas
RangeFormula
Q1:Q12Q1=SORT(P1:P12)
R1:R12R1=IF(ISTEXT(Q1:Q12),VALUE(LEFT(Q1:Q12,LEN(Q1:Q12)-1)),Q1:Q12)
T1:T12T1=SORTBY(Q1#,S1:S12,1)
S1:S12S1=RANK.EQ(R1,R$1#,1)
Dynamic array formulas.
 
Upvote 0
Here is another way, using one helper column, assuming that all your numeric portions are exactly 3 or 4 numbers long.

Let's say that your values are in column A, and you first value is in cell A1.
Then put this formula in cell B1 and copy down for all values in column A:
Excel Formula:
=IF(LEN(A1)<4,TEXT(A1,"0000"),IF(AND(LEN(A1)=4,ISNUMBER(RIGHT(A1,1)+0)=FALSE),"0" & A1,TEXT(A1,"0000")))
Then, if you sort by this "helper" column, your values will be in the order that you want.
 
Upvote 0
I will be creating a macro which generates a user form and then adds the new entered number to the list column

After adding the data, run the following macro SortNumbersWithLetters to sort the data in the same column.

Copy all the code in the same module:
VBA Code:
Option Explicit

Dim numbers As New Collection

Sub SortNumbersWithLetters()
  Dim c As Range
  Dim a As Variant, n As Variant, m As Variant, x As Variant
 
  Set numbers = Nothing
  a = Range("A2", Range("A" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    n = Val(a(i, 1))
    m = Mid(a(i, 1), Len(n) + 1)
    addnum Format(n, "000000000") & m
  Next
 
  i = 1
  For Each x In numbers
    n = Val(x)
    m = Mid(x, 10)
    b(i, 1) = Val(n) & m
    i = i + 1
  Next
  Range("A2").Resize(UBound(b)).Value = b
End Sub

Sub addnum(c)
  Dim i As Long
  For i = 1 To numbers.Count
    Select Case StrComp(numbers(i), c, vbTextCompare)
    Case 0, 1: numbers.Add c, Before:=i: Exit Sub
    End Select
  Next
  numbers.Add c 'add to end
End Sub
 
Upvote 0
Given @DanteAmor 's vba solution, this is probably of little value to the OP but, I post it regardless.

Not restricted to just 3 or 4 numbers and using only one Helper column.
It does however assume only a single Alpha character suffix.
Sort on Helper column B

MRXLMAY21.xlsm
AB
1Original DataHelper
2907907
3907A907.02
412131213
512151215
613051305
713061306
815081508
91508A1508.02
101508B1508.04
111508Z1508.52
1216911691
1317101710
1417111711
151508215082
1615082C15082.06
check list
Cell Formulas
RangeFormula
B2:B16B2=IF(ISNUMBER(1*A2),A2,MID(A2,1,LEN(A2)-1)+((CODE(RIGHT(A2,1))-64)*0.02))
 
Upvote 0
After adding the data, run the following macro SortNumbersWithLetters to sort the data in the same column.

Copy all the code in the same module:
VBA Code:
Option Explicit

Dim numbers As New Collection

Sub SortNumbersWithLetters()
  Dim c As Range
  Dim a As Variant, n As Variant, m As Variant, x As Variant
 
  Set numbers = Nothing
  a = Range("A2", Range("A" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    n = Val(a(i, 1))
    m = Mid(a(i, 1), Len(n) + 1)
    addnum Format(n, "000000000") & m
  Next
 
  i = 1
  For Each x In numbers
    n = Val(x)
    m = Mid(x, 10)
    b(i, 1) = Val(n) & m
    i = i + 1
  Next
  Range("A2").Resize(UBound(b)).Value = b
End Sub

Sub addnum(c)
  Dim i As Long
  For i = 1 To numbers.Count
    Select Case StrComp(numbers(i), c, vbTextCompare)
    Case 0, 1: numbers.Add c, Before:=i: Exit Sub
    End Select
  Next
  numbers.Add c 'add to end
End Sub
Dante,

The above worked great after I declared "i" as an integer.

My next hurdle is that I have column A defined as my reference Column. Then the user will enter the desired number of columns to create for the table. That number resides in cell CC1. How can I expand my sort range to include column A plus the number of columns listed in cell CC1? So if the user enters 8 there would be 9 total columns to sort (column A + 8 more).
 
Upvote 0
Another question.
In column A do you have unique numbers or are there repeating values?
 
Upvote 0
If all of the above is correct, then try the following:

VBA Code:
Sub SortNumbersWithLetters()
  Dim c As Range
  Dim a As Variant, n As Variant, m As Variant, x As Variant
  Dim i As Long, j As Long, k As Long
  Dim col As Long
  
  Set numbers = Nothing
  col = Range("CC1").Value + 1
  
  a = Range("A2", Cells(Range("A" & Rows.Count).End(3).Row, col)).Value
  ReDim b(1 To UBound(a), 1 To col)
  For i = 1 To UBound(a)
    n = Val(a(i, 1))
    m = Mid(a(i, 1), Len(n) + 1)
    addnum Format(n, "000000000") & m
  Next
  
  i = 1
  For Each x In numbers
    n = Val(x)
    m = Mid(x, 10)
    b(i, 1) = Val(n) & m
    For j = 1 To UBound(a)
      If "" & a(j, 1) = b(i, 1) Then
        For k = 2 To UBound(a, 2)
          b(i, k) = a(j, k)
        Next
        Exit For
      End If
    Next
    i = i + 1
  Next
  Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

Sub addnum(c)
  Dim ii As Long
  For ii = 1 To numbers.Count
    Select Case StrComp(numbers(ii), c, vbTextCompare)
    Case 0, 1
      numbers.Add c, Before:=ii
      Exit Sub
    End Select
  Next
  numbers.Add c 'add to end
End Sub
 
Upvote 0
Sorry for that, I forgot to declare i as long.



I do not know if I understood correctly, if the user puts 8 then we must order from A to I?
Column A will have only unique values, no repeating values in column A. When the file is initially opened, a userform is launched and asks the user to enter the number of panels to configure (8 for example). Column A is already labeled as Reference Column. The added columns are labeled as Panel 1, Panel 2 ... Panel 8. When the user is entering data they are given a userform and they enter the block number (CC1) and then the panel number (CA1). The first part of macro searches column A for the block number (CC1) if one is not found, it adds it to the bottom of column A and then also adds it to the column found in CA1. If the block is found in column A, it will just add the block to the panel in CA1.
If all of the above is correct, then try the following:

VBA Code:
Sub SortNumbersWithLetters()
  Dim c As Range
  Dim a As Variant, n As Variant, m As Variant, x As Variant
  Dim i As Long, j As Long, k As Long
  Dim col As Long
 
  Set numbers = Nothing
  col = Range("CC1").Value + 1
 
  a = Range("A2", Cells(Range("A" & Rows.Count).End(3).Row, col)).Value
  ReDim b(1 To UBound(a), 1 To col)
  For i = 1 To UBound(a)
    n = Val(a(i, 1))
    m = Mid(a(i, 1), Len(n) + 1)
    addnum Format(n, "000000000") & m
  Next
 
  i = 1
  For Each x In numbers
    n = Val(x)
    m = Mid(x, 10)
    b(i, 1) = Val(n) & m
    For j = 1 To UBound(a)
      If "" & a(j, 1) = b(i, 1) Then
        For k = 2 To UBound(a, 2)
          b(i, k) = a(j, k)
        Next
        Exit For
      End If
    Next
    i = i + 1
  Next
  Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

Sub addnum(c)
  Dim ii As Long
  For ii = 1 To numbers.Count
    Select Case StrComp(numbers(ii), c, vbTextCompare)
    Case 0, 1
      numbers.Add c, Before:=ii
      Exit Sub
    End Select
  Next
  numbers.Add c 'add to end
End Sub
Everything is working great! Thanks for all the help.
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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