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.
 
ur not specifying ur office.
Here are 2 options with a formula / helper column.
Book5 (version 1).xlsb
ABCD
1Original DataSorted Data (ideal)Helper With LET
29071907907
3907A2907907
41213312131213
51215412151215
61305513051305
71306613061306
81508715081508
91508A815081508
101508B915081508
1116911016911691
1217101117101710
1317111217111711
Sheet2
Cell Formulas
RangeFormula
C2:C13C2=--(CONCAT(FILTER(ISNUMBER(MID(A2,SEQUENCE(,LEN(A2),1,1),1)+0)*MID(A2,SEQUENCE(,LEN(A2),1,1),1),ISNUMBER(MID(A2,SEQUENCE(,LEN(A2),1,1),1)+0))))
D2:D13D2=LET( Chars,MID(A2,SEQUENCE(,LEN(A2),1,1),1), NumberCheck,ISNUMBER(MID(A2,SEQUENCE(,LEN(A2),1,1),1)+0), --(CONCAT(FILTER(NumberCheck*Chars,NumberCheck))))
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Dante,

I ran into an issue with my spreadsheet with this macro. The table has a cell with a five digit number 21003. This appeared in Column A, my reference column, as well as in column B, which is used for Panel 1. The user then wanted to add a new block number in the column for Panel 2 (col C) and the block number was 1003. When they selected OK on my user form, it added it to column C for panel 2 correctly but it added it on the row for 21003 instead of creating a new row for 1003. How to I require the macro to find an exact match to the block being added to prevent the new block being added to a row that contains the number but is not an exact match? From how I understand your original macro to work, it should be able to handle nine digit numbers along with letters.

The macro I am currently running on the spreadsheet is listed below.


MACRO CODE:

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
  Dim i As Long, j As Long, k As Long
  Dim col As Long
 
  Set numbers = Nothing
  col = Sheets("Block Tracking Multiple").Range("CF1").Value + 1
 
  a = Range("A5", 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("A5").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
I wrote the code below after reading the original question and not realizing this was an "old" question that was revived. The code below answers your original question and also handles values that are not all 4-digits long. I am unclear about your "panels" so I don't think this code addresses them. If you can adapt the code, fine, if not, please clarify what is supposed to happen with the "panels" (are they being sorted? sorted individually without regard to other columns? sorted as one big bunch? something else?).
VBA Code:
Sub SortNumbersWithLetters()
  With Range("A2", Cells(Rows.Count, "A").End(xlUp))
    .NumberFormat = "@"
    .Value = Evaluate(Replace("TEXT(IF(ISNUMBER(-RIGHT(@)),@,SUBSTITUTE(@,RIGHT(@),"".""&CODE(RIGHT(@)))),""00000000.00"")", "@", .Address))
    .Sort Columns("A"), xlAscending
    .NumberFormat = "General"
    .Value = Evaluate(Replace("IF(ISNUMBER(FIND("".00"",@)),0+@,SUBSTITUTE(0+@,RIGHT(@,3),CHAR(0+RIGHT(@,2))))", "@", .Address))
  End With
End Sub
 
Last edited:
Upvote 0
How to I require the macro to find an exact match to the block being added to prevent the new block being added to a row that contains the number but is not an exact match?
I am not understanding well. The problem you have is with the way to sort the data or with the way your userform searches for data?
 
Upvote 0
I have copied a portion of the spreadsheet and included it below so that you can see hot the sheet is laid out. I will include additional portion of my code below as well.


Spreadsheet layout:

Panel Tracking Sheet - 3145.xlsm
ABCDEFGHI
2Cross Cabinet Connections
3Reference RowPanel 1Panel 2Panel 3Panel 4Panel 5Panel 6Panel 7Panel 8
5907907
6907A907A
712031203
81204B1204B
912051205
1012061206
1112091209
1221222122
1344024402
1412008A12008A
151400314003
161400414004
171400614006
181400914009
191401514015
202041020410
212041320413
2221003210031003
232100421004
2421008A21008A
2521009A21009A
262101021010
Block Tracking Multiple
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B3:AZ3Celldoes not contain a blank value textNO
D2:J2,L2:AZ2Expression=D3<>""textNO


Reference cells to capture data from Userform1:
Panel Tracking Sheet - 3145.xlsm
BZCACBCCCDCECF
1Panel2Block1003Total Panels14
Block Tracking Multiple



Userform1 Macro "OK" button code:

The MyRow find line of code is most likely where my issue lies. I do not know how to require an exact match on this line of code. I believe that a correction on this line would solve the problem.

VBA Code:
Private Sub CommandButton2_Click()

UserForm1.Hide

Dim LastRow As Long
Dim MyRow As Range
Dim x As Integer


LastRow = Cells(Rows.Count, 1).End(xlUp).Row

With Sheets("Block Tracking Multiple")
    Set MyRow = .Range("A5:A" & LastRow).Find(What:=Range("CC1").Value, LookIn:=xlValues)
End With

If MyRow Is Nothing Then
    Range("A" & LastRow + 1).Value = Range("CC1").Value

'   Paste value in correct column
    Range("A" & LastRow + 1).Offset(0, Range("CA1").Value).Value = Range("CC1").Value
  
ElseIf MyRow > 0 Then
    Cells(MyRow.Row, Range("CA1").Value + 1).Value = Range("CC1").Value
  
End If

Call SortNumbersWithLetters

Call ADD_Border

End Sub
 
Upvote 0
Change this line:
VBA Code:
Set MyRow = .Range("A5:A" & LastRow).Find(What:=Range("CC1").Value, LookIn:=xlValues)

For this line:
VBA Code:
Set MyRow = .Range("A5:A" & LastRow).Find(What:=Range("CC1").Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True)

Note:
MatchCase:=True is case sensitive. If it doesn't matter if it is uppercase or lowercase then change to False
 
Upvote 0
Thank you all for you time. After some additional digging on my part, I found my error in the code. I needed to add "lookat:=xlwhole" to my find line to force the exact match. Once I did that, I am back in business. I appreciate the help and support of this community as I find you all to be massive excel wizards and hope to be a resource half as good as you someday.
 
Upvote 0
Dante, I didn't even see your response until my browser refreshed. I add the "MatchCase:=True" component to the line to ensure I am forcing the exact match. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,945
Members
449,095
Latest member
nmaske

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