Horizontal sorting of names

mjf269

New Member
Joined
Jul 26, 2011
Messages
2
I need to get excel to sort the names i have in a selection of cells horizontally. The current worksheet has 10 columns but can have an unknown number of rows. For example (i will keep it limited to 5 columns).

agroup, bgroup, cgroup, dgroup, fgroup
hgroup, igroup, jgroup, kgroup, lgroup,
mgroup, egroup, ggroup

should come out as:

agroup, bgroup, cgroup, dgroup, egroup
fgroup, ggroup, hgroup, igroup, jgroup
kgroup, lgroup, mgroup

I appreciate any help you can provide on this. Thanks in advance.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Well, i was able to work this out on my own with some help from Microsoft support. You just have to select the cells you want to sort. Here is the code:

Sub SortAllRangeData()
' Place column header for temporary sort area.
Range("IV1").Value = "Numbers"

' Move numbers to temporary sort location.
For Each cell In Selection
Range("iv65536").End(xlUp).Offset(1, 0) = cell.Value
Next cell

' Sort numbers in ascending order.
Range("IV2", Range("IV2").End(xlDown)).Sort Key1:=Range("IV2"), _
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

' Move sorted data back to original sheet location.
Selection(1, 1).Activate ' Make sure the ActiveCell is the
' top left of Selection first.
CCnt = Selection.Columns.Count
RCnt = Selection.Rows.Count
CellCnt = Selection.Cells.Count
Tcell = 2
For r = 1 To RCnt
For c = 1 To CCnt
Range(ActiveCell.Address).Offset(r - 1, c - 1).Value = _
Range("iv" & Tcell).Value
Tcell = Tcell + 1
Next c
Next r

' Clean up temporary sort location.
Range("IV1", Range("IV1").End(xlDown)).Clear
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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