ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sorting Multiple Columns in a single operation? (https://www.excelbanter.com/excel-programming/385168-sorting-multiple-columns-single-operation.html)

Andy Hulse

Sorting Multiple Columns in a single operation?
 
We have collected approx 100 pieces of info on 250 students, each held
student id being the column header and the data held in that column. We want
to be able to select all the data and to be able to sort each column into
ascending or descending order, independently of the other columns in a single
opeation. (i.e. the equivalent of selecting each column of data, sorting it,
then selecting the next etc., until all hav been slected and sorted). Can
anyone suggest how this may be done, is there a VBA script out there that can
do this?


merjet

Sorting Multiple Columns in a single operation?
 
The following does 4 columns and assumes the same number of rows in
each column. You can easily modify it to suit.

Sub Macro1()
Dim iEnd As Integer
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
iEnd = ws.Range("A1").End(xlDown).Row
For iCol = 1 To 4
ws.Range(Cells(1, iCol), Cells(iEnd, iCol)).Sort Key1:=Cells(2,
iCol), _
Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Next iCol
End Sub

Hth,
Merjet



Andy Hulse[_2_]

Sorting Multiple Columns in a single operation?
 
Thanks a lot for this, with a bit of fiddling I have managed to get it to do
just what I needed.

Great and thanks once again

"merjet" wrote:

The following does 4 columns and assumes the same number of rows in
each column. You can easily modify it to suit.

Sub Macro1()
Dim iEnd As Integer
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
iEnd = ws.Range("A1").End(xlDown).Row
For iCol = 1 To 4
ws.Range(Cells(1, iCol), Cells(iEnd, iCol)).Sort Key1:=Cells(2,
iCol), _
Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Next iCol
End Sub

Hth,
Merjet





All times are GMT +1. The time now is 05:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com