Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Sorting the contents of a cell

I have a range of cells. Each cell can contain multiple
entries separated by a comma and a space.

Example: cell A2 contains: John, Adam, Charlie

How can I sort the contens of the cell so I get:

A2 = Adam, Charlie, John

Thanks,

Maggie B.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Sorting the contents of a cell

You can use the split command to convert the content to an array, then sort
the array, use the join command to recreate, then place it back in the cell.

Sub SortCell()
Dim i as Long, j as Long
Dim swap1, swap2
Dim varr
varr = Split(ActiveCell, ",")
' peform bubble sort
For i = 0 To UBound(varr) - 1
For j = i + 1 To UBound(varr)
varr(i) = Trim(varr(i))
varr(j) = Trim(varr(j))
If varr(i) varr(j) Then
Swap1 = varr(i)
Swap2 = varr(j)
varr(j) = Swap1
varr(i) = Swap2
End If
Next j
Next i
ActiveCell.Value = Application.Trim( _
Join(varr, ", "))
End Sub

Assumes Excel 2000 or later.

--
Regards,
Tom Ogilvy


"Maggie B." wrote in message
...
I have a range of cells. Each cell can contain multiple
entries separated by a comma and a space.

Example: cell A2 contains: John, Adam, Charlie

How can I sort the contens of the cell so I get:

A2 = Adam, Charlie, John

Thanks,

Maggie B.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate contents of cells whose contents resemble cell referem cathyh Excel Worksheet Functions 3 May 23rd 09 12:16 PM
sorting contents from right to left Kim Excel Worksheet Functions 1 June 7th 07 01:51 PM
Sorting on partial Cell contents VBA EagleOne Excel Discussion (Misc queries) 2 November 21st 06 05:14 PM
Sorting and having cell formats stick with contents Paul Excel Discussion (Misc queries) 3 August 5th 05 03:19 AM
Macro to remove contents of cell and move all other contents up one row adw223 Excel Discussion (Misc queries) 1 July 1st 05 03:57 PM


All times are GMT +1. The time now is 02:50 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"