Sort Contents Within 1 Cell

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
331
Office Version
  1. 365
Platform
  1. Windows
I have a list of values that is seperated by a return. How do I sort each cell in alphabetical order without moving the row?

EXAMPLE.
CURRENT DATA
[TABLE="width: 500"]
<tbody>[TR]
[TD]1[/TD]
[TD]APPLES
GRAPES
WATERMELON
BANANA
CHEERY[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]PINK
BLUE
GREEN
YELLOW[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]APRIL
AUGUST
JULY
DECEMBER
JUNE[/TD]
[/TR]
</tbody>[/TABLE]

WANTED RESULTS

[TABLE="width: 500"]
<tbody>[TR]
[TD]1[/TD]
[TD]APPLES
BANANA
CHERRY
GRAPES
WATERMELON[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]BLUE
GREEN
PURPLE
YELLOW[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]APRIL
AUGUST
JULY
JUNE
DECEMBER
[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Not sure if there's a worksheet function to do this...

With this method, you would select a range to have sorted, then run the code.

Code:
Sub AlphaSelection()  'select range to sort then run

Dim cel As Range, txt$
Dim Arr As Variant, i&, aTemp As Variant
Dim coll As Object
Set coll = CreateObject("System.Collections.ArrayList")


For Each cel In Selection.Cells
    Arr = Split(cel.Value, Chr(10))
    For i = LBound(Arr) To UBound(Arr)
        coll.Add CStr(Arr(i))
    Next i
    coll.Sort
    aTemp = coll.ToArray
    cel.Value = Join(aTemp, Chr(10))
    Erase Arr
    Erase aTemp
    coll.Clear
Next
End Sub
 
Upvote 0
I can not think of a way to do do that without VBA.
Select the cells that you want and run the macro.
The delimiter of the carriage return is hardcoded in. CHR(10)

Code:
Public Sub SortVals()    Dim i, a, b As Integer
    Dim arr As Variant
    Dim delimiter As String
    Dim cel As Range
    Dim selectedRange As Range
    Set selectedRange = Application.Selection
'loop through the selected cells
    For Each cel In selectedRange.Cells
        If cel.Value = vbNullString Then GoTo nextCell
        arr = Split(cel, Chr(10))
        ' trim values so sort will work properly
        For i = LBound(arr) To UBound(arr)
            arr(i) = Trim(arr(i))
        Next i
        ' sort
        QuickSort arr, LBound(arr), UBound(arr)
   
 ' load sorted values back to cell
        delimiter = ""
        cel.Value = ""
        For b = LBound(arr) To UBound(arr)
            cel.Value = cel.Value & delimiter & CStr(arr(b))
            delimiter = Chr(10)
        Next b
nextCell:
    Next cel


End Sub


Public Sub QuickSort(vArray As Variant, inLow As Long, inHi As Long)


  Dim pivot   As Variant
  Dim tmpSwap As Variant
  Dim tmpLow  As Long
  Dim tmpHi   As Long


  tmpLow = inLow
  tmpHi = inHi


  pivot = vArray((inLow + inHi) \ 2)


  While (tmpLow <= tmpHi)


     While (vArray(tmpLow) < pivot And tmpLow < inHi)
        tmpLow = tmpLow + 1
     Wend


     While (pivot < vArray(tmpHi) And tmpHi > inLow)
        tmpHi = tmpHi - 1
     Wend


     If (tmpLow <= tmpHi) Then
        tmpSwap = vArray(tmpLow)
        vArray(tmpLow) = vArray(tmpHi)
        vArray(tmpHi) = tmpSwap
        tmpLow = tmpLow + 1
        tmpHi = tmpHi - 1
     End If


  Wend


  If (inLow < tmpHi) Then QuickSort vArray, inLow, tmpHi
  If (tmpLow < inHi) Then QuickSort vArray, tmpLow, inHi


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,749
Messages
6,180,725
Members
452,995
Latest member
isldboy

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