Forum Discussion
dankesselring
Oct 21, 2024Copper Contributor
Sort Order
How do I sort data in a column that has blanks, alpha characters, and numbers in that order?
HansVogelaar
Oct 21, 2024MVP
Excel does not offer a built-in way to do that. You might use a helper column and a custom VBA function. It will work in the desktop version of Excel for Windows and Mac.
For Windows:
Press Alt+F11 to activate the Visual Basic Editor.
Select Insert > Module.
Copy the following code into the new module:
Function SortSpecial(ByVal s) As String
' Based on code by David McRitchie
Const FromSTR = " -/=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Const SortSTR = "#$()0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dim i As Long, j As Long
s = UCase(s)
For i = 1 To Len(s)
For j = 1 To Len(FromSTR)
If Mid(s, i, 1) = Mid(FromSTR, j, 1) Then
SortSpecial = SortSpecial & Mid(SortSTR, j, 1)
Exit For
End If
Next j
Next i
SortSpecial = "z" & SortSpecial
End Function
Switch back to Excel.
Let's say your data are in A2:A40.
Select B2:B40.
Enter the formula =SortSpecial(A2) and confirm the formula by pressing Ctrl+Enter.
Select B2.
Sort from A to Z the usual way.
Save your workbook as a macro-enabled workbook (*.xlsm).
Make sure that you allow macros when you open it.