Markus,
The best way is to use a Custom User-Defined-Function. See the code
below. Can either be used in VBA or as a Worksheet function. If used
as a worksheet function, use it like:
=SortCell(A1, TRUE)
to return
1, 2, 3, 4, 5, 6
The macro TryIt shows how to use it in VBA - assumes the input string
is in cell A1.
Both require Excel2000 or later to function, due to the Split and Join
commands.
HTH,
Bernie
MS Excel MVP
Function SortCell(InCell As Range, myBool As Boolean) As Variant
SortCell = Join(ArrayBubbleSort(Split(InCell.Value, ", "), myBool), ",
")
End Function
Sub TryIt()
MsgBox Join(ArrayBubbleSort(Split(Range("A1").Value, ", "), True), ",
")
End Sub
Function ArrayBubbleSort( _
myArray As Variant, _
Optional Ascending As Boolean) _
As Variant
Dim myTemp As Variant
Dim i As Integer
Dim j As Integer
'Do the sort
For i = LBound(myArray) To UBound(myArray) - 1
For j = i + 1 To UBound(myArray)
If Ascending Then
If myArray(i) > myArray(j) Then
myTemp = myArray(j)
myArray(j) = myArray(i)
myArray(i) = myTemp
End If
Else
If myArray(i) < myArray(j) Then
myTemp = myArray(j)
myArray(j) = myArray(i)
myArray(i) = myTemp
End If
End If
Next j
Next i
'Return the array
ArrayBubbleSort = myArray
End Function