Article ID: 126931
Article Last Modified on 1/31/2007
A1: 1
A2: 12
A3: 1A1
A4: 1A2
A5: 2
' Assume a cell entry of 1A 'myvalue' is the cell reference of the
' number to be sorted.
Function numsort(myvalue As Variant)
' Calculates the function any time the worksheet recalculates.
Application.Volatile
' Initializes the variable count as 0.
Count = 0
' If the cell is numeric, the variable 'count' is equal to the value
' in the cell times 1000.
' 1A is not numeric
If IsNumeric(myvalue) Then
Count = myvalue * 1000
Else
no_text_yet = True
' Sets a For-Next loop from 1 to the length of characters in the
' cell.
' With 1A, the For-next loop will be from 1 to 2.
For x = 1 To Len(myvalue)
' Sets the variable 'current' to the character of position x,
' for a length of 1 character.
' The first time through, 'current' will equal 1.
' The second time through, 'current' will equal A.
current = Mid(myvalue, x, 1)
If IsNumeric(current) Then
' If 'current' is numeric, then 'count' is equal to itself
' times ten plus 'current'.
' The first time through, 'count' is numeric and will equal
' 1.
' (0 * 1 + 1).
' The second time through, A is not numeric.
Count = Count * 10 + current
' If 'current' is not numeric, then 'count' is equal to itself
' times 1000 plus the ASCII value of the letter.
' The first time through, 1 is numeric.
' The second time through, 'count' equals itself(1) times 1000
' + the ASCII character value of A(65), or 1065
Else
Count = Count * 1000 + Asc(current)
' Exits the For-Next loop as soon as we reach the first alpha
' character
Exit For
End If
Next
' If the For-Next loop variable is not equal to the length of
' characters of myvalue then the last characters must be calculated.
If x <> Len(myvalue) Then Count = _
Count + Right(myvalue, Len(myvalue) - x) * 0.001
' This will happen as soon as we encounter the first alpha
' character. In this is the case, 'count' equals itself plus the
' right character of the total length minus the For-Next variable
' "x".
End If
numsort = Count
End Function
A1: 1
A2: 12
A3: 1A1
A4: 1A2
A5: 2
A1: 1
A2: 2
A3: 12
A4: 1A1
A5: 1A2
B1: =numsort(A1)
A1: 1 B1: 1000
A2: 2 B2: 2000
A3: 12 B3: 12000
A4: 1A1 B4: 1065.001
A5: 1A2 B5: 1065.002
A1: 1 B1: 1000
A2: 1A1 B2: 1065.001
A3: 1A2 B3: 1065.002
A4: 2 B4: 2000
A5: 12 B5: 12000
Additional query words: 5.0 5.00a 5.00c 8.00 97 sort weird wrong incorrect XL5 XL95 XL97 alpha-numeric XL
Keywords: kbdtacode kbprb KB126931