I have a need to sort, a column, with cells that contain both
letters/characters and numbers. When the cell has a combination of
characters and numbers, I would like Excel to ignor any character that
is NOT a number and then sort the cells.
Example of data
6
[6
6]
[6]
7
12
14
14]
The above example 'shows' the resulting sort that I am looking for. I
need to keep the data displayed with the extra characters.
Any help out there?
I think you will require helper columns for both the numeric and non-numeric
portions of your data. Then you sort first on the numeric column and second on
the non-numeric column.
However, since the sorting you show above is not the way an Excel ASCII sort of
the non-numeric characters would normally come out, I had to make some changes.
These may or may not be satisfactory in all cases so you'll have to check.
In order to obtain the above sort order, I added a leading <space> to the
non-numeric portion if there was only a single non-numeric character.
I used to UDF's, one to extract the numeric portion, and the second to extract
the non-numeric portion. I entered these in two helper columns and then sorted
ascending first on the numeric, and then on the non-numeric.
To enter the UDF's, <alt-F11> opens the VB Editor. Ensure your project is
highlighted in the project explorer, then Insert/Module and paste the code
below into the window that opens.
Then in one column insert the formula:
=getnums(A1)
and in another column the formula:
=getstr(A1)
Change A1 to reflect the address of your first entry.
Copy/drag the formulas down as far as necessary.
Then Data/Sort
Ensure the column headers is appropriate for your data
Sort first by (whatever column has the extracted numbers)
and Then by (whatever column has the extracted text).
Finally, hide or delete the two helper columns.
==================================
Option Explicit
Function GetNums(str)
Dim N As Integer, i As String
i = ""
For N = 1 To Len(str)
If IsNumeric(Mid(str, N, 1)) Then
i = i & Mid(str, N, 1)
If Mid(str, N + 1, 1) = "." Then i = i & "."
End If
Next
If i = "" Then
GetNums = i
Exit Function
End If
GetNums = CDbl(i)
End Function
Function GetStr(str) As String
GetStr = ""
Dim N As Integer
For N = 1 To Len(str)
If Not (IsNumeric(Mid(str, N, 1))) Then GetStr = GetStr & Mid(str, N,
1)
Next
If Len(GetStr) = 1 Then GetStr = " " & GetStr
End Function
======================================
--ron