Sort multiple columns

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
Book1
ABCDEFGHIJKL
1PSTMSTCSTEST
2LASLGBASETUSAMADFWATLSHV
3LAXSFOCYSABQAUSDALCAELCH
4OAKPSPDENCRPIAHCLTBTR
5SANSMFDROHOULBBEWRAEX
6SEASJCGJTLNKSATFLL
7GUCMSPGSO
8LASOMAJFK
9PHXORDLGA
10SLCSTLMCO
Sheet1


Is there a way to sort the items automatically under each header.

ex.
Book1
ABCDEFGHIJKL
1PSTMSTCSTEST
2LASSJCABQSLCAMASATATLLCH
3LAXASETUSAUSSTLCAESHV
4LGBCYSCRPCLT
5OAKDENHOUEWR
6PSPDROLBBFLL
7SANGJTLNKGSO
8SEAGUCMSPJFK
9SFOLASOMALGA
10SMFPHXORDMCO
Sheet1



I tried using this code but it does not sort the other columns under each header..

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Range("A2:C10").Sort Key1:=Range("A2"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    Range("D2:F10").Sort Key1:=Range("D2"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    Range("G2:I10").Sort Key1:=Range("G2"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    Range("J2:L10").Sort Key1:=Range("J2"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

End Sub
 
Last edited by a moderator:

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Code:
For i = 1 To Cells(2, Columns.Count).End(xlToLeft).Column
    Range(Cells(2, i), .Cells(Rows.Count, i).End(xlUp)).Sort Cells(2, i), 1
Next
 
Upvote 0
Code:
For i = 1 To Cells(2, Columns.Count).End(xlToLeft).Column
    Range(Cells(2, i), .Cells(Rows.Count, i).End(xlUp)).Sort Cells(2, i), 1
Next

It gives me an error..

invalid or unqualified reference

any other suggestion?
 
Upvote 0
delete "."
Code:
Range(Cells(2, i), Cells(Rows.Count, i).End(xlUp)).Sort Cells(2, i), 1
 
Upvote 0
delete "."
Code:
Range(Cells(2, i), Cells(Rows.Count, i).End(xlUp)).Sort Cells(2, i), 1

Almost there...

The headers should not be included in the sorting, because I get an error stating the cells need to be identical. What do I need to change so the headers are not included in the sorting?
 
Upvote 0
Is there a better way to write this code? I've concluded the code recommended inteferes with other parts of my spreadsheet.

Private Sub Worksheet_Change(ByVal Target As Range)
Sheet6.Unprotect Password:="T0nyul!a"
Range("B7:J37").Sort Key1:=Range("C7"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AA5:AA37").Sort Key1:=Range("AA5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AB5:AB37").Sort Key1:=Range("AB5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AC5:AC37").Sort Key1:=Range("AC5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AD5:AD37").Sort Key1:=Range("AD5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AE5:AE37").Sort Key1:=Range("AE5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AF5:AF37").Sort Key1:=Range("AF5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AG5:AG37").Sort Key1:=Range("AG5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AH5:AH37").Sort Key1:=Range("AH5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AI5:AI37").Sort Key1:=Range("AI5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AJ5:AJ37").Sort Key1:=Range("AJ5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AK5:AK37").Sort Key1:=Range("AK5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Range("AL5:AL37").Sort Key1:=Range("AL5"), Order1:=xlAscending, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
Sheet6.Protect Password:="T0nyul!a"
End Sub
 
Upvote 0
Av8tordude said:
Almost there...
The headers should not be included in the sorting, because I get an error stating the cells need to be identical. What do I need to change so the headers are not included in the sorting?

Don't make me laugh...
If your sheet lyout posted is different from what you really have then never solved...
Av8tordude said:
Private Sub Worksheet_Change(ByVal Target As Range)
Sheet6.Unprotect Password:="T0nyul!a"
Range("B7:J37").Sort Key1:=Range("C7"), Order1:=xlAscending, Orientation:=xlTopToBottom,
 
Upvote 0
Don't make me laugh...
If your sheet lyout posted is different from what you really have then never solved...

My apologies...but would you agree to try on a test sheet then on the original project?
 
Upvote 0

Forum statistics

Threads
1,215,222
Messages
6,123,704
Members
449,118
Latest member
MichealRed

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