View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
[email protected] robingSA@gmail.com is offline
external usenet poster
 
Posts: 5
Default Sorting numbers within a cell

Thanks Joel. It returned a Compile Error: Expected Array for:

iArr(i) = in this section:

For i = 0 To UBound(sArr)
iArr(i) = Val(sArr(i))


On May 21, 12:49 pm, Joel wrote:
try this code. the sort alogorithm you are using didn't do a full osrt. I
changed the sort algorithm. I also convert the string to a number and then
back to a string to gett rid of the leading zeroes problem. If you need to
put zeroes in front of the numbers then changge as follows:

from
sOut = sOut & DELIM & CStr(iArr(i))
to
sOut = sOut & DELIM & Format(iArr(i), "000")

Public Sub SortNumbersWithinCells()
Const DELIM As String = ","
Dim sIn As String
Dim sArr As Variant
Dim iArr As Integer
Dim sOut As String
Dim temp As Double
Dim change As Boolean
Dim i As Integer
Dim cell As Range

For Each cell In Selection
sIn = cell.Text
sArr = Split(sIn, DELIM)
For i = 0 To UBound(sArr)
iArr(i) = Val(sArr(i))
Next i
For i = 0 To (UBound(sArr) - 1)
For j = 1 To UBound(sArr)

If iArr(i) iArr(j) Then
temp = iArr(i)
iArr(j) = iArr(i)
iArr(i) = temp
End If
Next j
Next i
For i = 0 To UBound(iArr)
sOut = sOut & DELIM & CStr(iArr(i))
Next i
cell.Value = Mid(sOut, 2)
sOut = Empty
Next cell
End Sub

" wrote:
Hi...I am wanting to sort a series of numbers contained in a single
cell, with each number separated by a comma. I found the code below
in a search, but it doesn't seem to recognise the DELIM part. I am
using Excel 2003 and would appreciate any other solutions. First
prize would be a sort that doesn't put 125 before 2 for example, but
if need be, I can enter the numbers as 002,025,125 etc to negate this
problem.


Many thanks.


Rob


Public Sub SortNumbersWithinCells()
Const DELIM As String = ","
Dim sIn As String
Dim sArr As Variant
Dim sOut As String
Dim temp As Double
Dim change As Boolean
Dim i As Integer
Dim cell As Range


For Each cell In Selection
sIn = cell.Text
sArr = Split(sIn, DELIM)
Do
change = False
For i = 0 To UBound(sArr) - 1
If sArr(i) sArr(i + 1) Then
temp = sArr(i)
sArr(i) = sArr(i + 1)
sArr(i + 1) = temp
change = True
End If
Next i
Loop Until change = False
For i = 0 To UBound(sArr)
sOut = sOut & DELIM & sArr(i)
Next i
cell.Value = Mid(sOut, 2)
sOut = Empty
Next cell
End Sub