Alphabetize Columns

C

curt.dye

I'm looking to put columns in alphabetical order based upon their
content in a given row. This works, but it seems to be rather
inefficient. Is there a better way of doing this?

Sub AlphaColumns()
rw = 1
cStart = 1
cEnd = 10

For i = cStart To cEnd
For j = i To cEnd
If UCase(Cells(rw, i).Value) > UCase(Cells(rw, j).Value) Then
Cells(1, i).EntireColumn.Select
Selection.Cut
Cells(1, j + 1).EntireColumn.Select
Selection.Insert Shift:=xlToRight
i = Application.WorksheetFunction.Max(cStart, i - 1)
End If
Next j
Next i

End Sub
 
J

Joel

Sub AlphaColumns()

rw = 1
cStart = 1
cEnd = 10

Set SortRange = Range(Cells(rw, cStart), Cells(rw, cEnd))

SortRange.Sort _
Key1:=Cells(rw, cStart), _
Order1:=xlAscending, _
header:=xlNo, _
Orientation:=xlLeftToRight
End Sub
 
J

Jim Cone

Cells.Sort Key1:=Rows(rw), Order1:=xlAscending, Orientation:=xlLeftToRight
--
Jim Cone
Portland, Oregon USA





<[email protected]>
wrote in message
I'm looking to put columns in alphabetical order based upon their
content in a given row. This works, but it seems to be rather
inefficient. Is there a better way of doing this?

Sub AlphaColumns()
rw = 1
cStart = 1
cEnd = 10
For i = cStart To cEnd
For j = i To cEnd
If UCase(Cells(rw, i).Value) > UCase(Cells(rw, j).Value) Then
Cells(1, i).EntireColumn.Select
Selection.Cut
Cells(1, j + 1).EntireColumn.Select
Selection.Insert Shift:=xlToRight
i = Application.WorksheetFunction.Max(cStart, i - 1)
End If
Next j
Next i
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top