Put this code into your workbook, it should do the trick.
Since this is a 'destructive' operation (values in cells are overwritten) I
recommend that you create a copy of your workbook to use to verify that it
works properly. Actually, you can simply create a copy of the worksheet and
put it in the same workbook to test with.
To put the code into a workbook: open the workbook, then press [Alt]+[F11]
to open the Visual Basic Editor (VBE). In the VBE use its menu to Insert |
Module and then copy the code into that module. Make any changes to column
identifier or starting row number (row with your 1st set of numbers to sort),
then close the VBE.
Select the sheet with the data on it, then from the Excel menu toolbar choose:
Tools | Macro | Macros and click on SortInCells and click the [Run] button.
It took my slow machine 4 or 5 seconds to do 45,000 rows of your sample
data: AMD 3200+ 1GB RAM, WinXP Pro, Excel 2003. Time will depend on actual
number of entries and number of numbers in each entry, and physical setup of
the computer you run it on.
Sub SortInCells()
'must have sheet with data on it selected
'when you call this macro
Const dataColumn = "A" ' change?
Const firstRowUsed = 1 ' change?
Dim toSort() As Integer
Dim lastRow As Long
Dim rngToSort As Range
Dim anyCell As Object
Dim splitOut As Variant
Dim lCount As Long ' loop counter
Dim tmpValue As Integer
Dim swappedFlag As Boolean
Dim humptyDumpty As String
lastRow = Range(dataColumn & Rows.Count).End(xlUp).Row
Set rngToSort = Range(dataColumn & firstRowUsed & ":" & _
dataColumn & lastRow)
For Each anyCell In rngToSort
If Not IsEmpty(anyCell) And InStr(anyCell.Value, ",") > 0 Then
splitOut = Split(anyCell.Value, ",")
ReDim toSort(LBound(splitOut) To UBound(splitOut))
'convert strings to numbers and put in array to sort
For lCount = LBound(splitOut) To UBound(splitOut)
toSort(lCount) = Val(splitOut(lCount))
Next
'Quicksort is a little slow for small groups of
'numbers, but if you've got longer lists, it'll
'be much faster than ones for short lists (like a bubble)
QuickSort toSort(), LBound(toSort), UBound(toSort)
'now we have to put the pieces back together as a string
humptyDumpty = "" ' clear any prior results
For lCount = LBound(toSort) To UBound(toSort)
humptyDumpty = humptyDumpty & Trim(Str(toSort(lCount))) & ","
Next
humptyDumpty = Left(humptyDumpty, Len(humptyDumpty) - 1)
anyCell.Value = humptyDumpty
End If
Next ' end of loop through rngToSort
End Sub
Private Sub QuickSort(list() As Integer, ByVal min As Long, ByVal max As Long)
'an implementation of a Quick Sort
'change the List() type to the type of data you will be sorting
'
Dim med_value As Long
Dim hi As Long
Dim lo As Long
Dim i As Long
' If min >= max, the list contains 0 or 1 items so it
' is sorted.
If min >= max Then
Exit Sub
End If
' Pick the dividing value.
i = Int((max - min + 1) * Rnd + min)
med_value = list(i)
' Swap it to the front.
list(i) = list(min)
lo = min
hi = max
Do
' Look down from hi for a value < med_value.
Do While list(hi) >= med_value
hi = hi - 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
list(lo) = med_value
Exit Do
End If
' Swap the lo and hi values.
list(lo) = list(hi)
' Look up from lo for a value >= med_value.
lo = lo + 1
Do While list(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
list(hi) = med_value
Exit Do
End If
' Swap the lo and hi values.
list(hi) = list(lo)
Loop
' Recursively sort the two sublists.
QuickSort list(), min, lo - 1
QuickSort list(), lo + 1, max
End Sub
Peter Herman said:
Hi there,
I have a column of numbers separated by commas that is about 45,000 rows
long. I would like to sort the numbers in each individual row from small to
large.
In other words my data looks like this:
column A
17,1,36,98,62
56,94,12,24
and so on...all the way down and I would like it to look like this:
Column A
1,17,36,62,98
12,24,56,94
Thanks to anyone who helps me.