Sort Contents Of Cell

Nanogirl21

Active Member
Joined
Nov 19, 2013
Messages
331
Office Version
  1. 365
Platform
  1. Windows
Is there a way to sort contents of a cell in alphabetical order? Each value that need to be sorted is seperated by , (comma space).

EXAMPLE IN COLUMN E:

RICE, CHEERIES, BANANAS, CUCUMBERS, BREAD

WANTED RESULTS IN COLUMN E:

BANANAS, BREAD, CHERRIES, CUCUMBERS, RICE
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Test this in a copy of your workbook (as it over-writes the original column E values as requested)

Code:
Sub SortCellValues()
  Dim AL As Object
  Dim a As Variant, sWord As Variant
  Dim i As Long
  
  Set AL = CreateObject("System.Collections.ArrayList")
  a = Range("E1", Range("E" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    AL.Clear
    For Each sWord In Split(a(i, 1), ", ")
      AL.Add sWord
    Next sWord
    AL.Sort
    a(i, 1) = Join(AL.ToArray, ", ")
  Next i
  Range("E1").Resize(UBound(a)).Value = a
End Sub
 
Upvote 0
Test this in a copy of your workbook (as it over-writes the original column E values as requested)

Code:
Sub SortCellValues()
  Dim AL As Object
  Dim a As Variant, sWord As Variant
  Dim i As Long
  
  Set AL = CreateObject("System.Collections.ArrayList")
  a = Range("E1", Range("E" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    AL.Clear
    For Each sWord In Split(a(i, 1), ", ")
      AL.Add sWord
    Next sWord
    AL.Sort
    a(i, 1) = Join(AL.ToArray, ", ")
  Next i
  Range("E1").Resize(UBound(a)).Value = a
End Sub

Worked perfect. Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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