Function to sort words in a cell almost works

kanigetts

New Member
Joined
Mar 3, 2011
Messages
4
I have a custom function to alphabetically sort words within a cell. I didn't write it and can't remember where I got it but it doesn't work perfectly. It will correctly sort comma delimited items except when there are only 2 values. When there are only 2 values it does nothing and leaves them in their original order. I am not a programmer, so I don't know what is going wrong. Here is the code:


Function StrSort(ByVal sInp As String, _
Optional bDescending As Boolean = False) As String
' sorts a comma-delimited string
Dim asSS() As String ' substring array
Dim sSS As String ' temp string for exchange
Dim n As Long
Dim i As Long
Dim j As Long

asSS = Split(sInp, ",")
n = UBound(asSS)

For i = 0 To n
asSS(i) = Trim(asSS(i))
Next

If n <= 1 Then
StrSort = sInp
Else
For i = 0 To n - 1
For j = i + 1 To n
If (asSS(j) < asSS(i)) Xor bDescending Then
sSS = asSS(i)
asSS(i) = asSS(j)
asSS(j) = sSS
End If
Next j
Next i
StrSort = Join(asSS, ", ")
End If
End Function</pre>
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Change "If n <= 1 Then" to "If n < 1 Then"

The original says that if the UBound of the array is 1 or less, then the function (StrSort) should simply return the input string (sInp). Well, when the UBound of a zero-based array is 1, there are two elements: 0 and 1. Changing the operand to "<" will return the original string if there is no comma (only one value provided).
 
Upvote 0
How are you calling the Formula/function?

Are you using it through VBA or you using it through excel sheets, if the latter please supply the formula you are using for referene
 
Upvote 0
Hopefully it's similar to:

=strsort("A,D,B,I,C")

or

=strsort(A10,TRUE)

The first will sort the string provided in ascending order. The second would sort the string in cell A10 in descending order, expecting a comma to delimit each value.
 
Upvote 0
Change this line of the function's code...

Code:
If n <= 1 Then

...to this:

Code:
If n < 1 Then

HTH

Robert
 
Upvote 0
Change "If n <= 1 Then" to "If n < 1 Then"

The original says that if the UBound of the array is 1 or less, then the function (StrSort) should simply return the input string (sInp). Well, when the UBound of a zero-based array is 1, there are two elements: 0 and 1. Changing the operand to "<" will return the original string if there is no comma (only one value provided).


MVPTomlinson has it all there,
Find
Code:
    If n <= 1 Then
        Str Sort = sInp
    Else
        For i = 0 To n - 1

To
Code:
    If n < 1 Then
        Str Sort = sInp
    Else
        For i = 0 To n - 1

Regards
 
Upvote 0
Thanks for the help and explanation. It worked perfectly. Fastest forum response ever! Here is the fixed function:
Code:
Function StrSort(ByVal sInp As String, _
                  Optional bDescending As Boolean = False) As String
    ' sorts a comma-delimited string
    Dim asSS()  As String    ' substring array
    Dim sSS     As String    ' temp string for exchange
    Dim n       As Long
    Dim i       As Long
    Dim j       As Long

    asSS = Split(sInp, ", ")
    n = UBound(asSS)

    For i = 0 To n
        asSS(i) = Trim(asSS(i))
    Next

    If n < 1 Then
        StrSort = sInp
    Else
        For i = 0 To n - 1
            For j = i + 1 To n
                If (asSS(j) < asSS(i)) Xor bDescending Then
                    sSS = asSS(i)
                    asSS(i) = asSS(j)
                    asSS(j) = sSS
                End If
            Next j
        Next i
        StrSort = Join(asSS, ", ")
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,673
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top