Forum

Notifications
Clear all

How to sort multiple columns?

5 Posts
2 Users
0 Reactions
49 Views
(@knotbright)
Posts: 3
Active Member
Topic starter
 

Attached is a sample section of my 200xcolumn database.

I'm looking for a way - formula or macro - to sort all columns from low to high, as I've manually done with the bolded column E.

I'm using Excel 2007.

Is it possible to automate this process and do all columns at once?

Thanks.

 
Posted : 24/09/2020 10:15 pm
(@questvba)
Posts: 125
Estimable Member
 

Hi Bob Ritchie,

Here is a solution

Option Explicit
Sub Sorting_()

Dim Plage As Range
Dim i As Byte
Dim LastColumn As Byte, LastRow As Long

With Sheets("Sheet1")

LastColumn = .Range("F7").End(xlToRight).Column
LastRow = .Range("F7").End(xlDown).Row
Application.ScreenUpdating = False

For i = 5 To LastColumn

Set Plage = .Range(Cells(7, i), Cells(LastRow, i))
Plage.Sort Key1:=.Cells(7, i), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

Next

Application.ScreenUpdating = True

End With
End Sub

 
Posted : 25/09/2020 12:20 am
(@knotbright)
Posts: 3
Active Member
Topic starter
 

Lionel Baijot, thanks for your response.

Unfortunately my knowledge of Excel is fairly basic and I'm not sure how to apply your solution.

Would you be kind enough to elaborate on that?

Thank you.

 
Posted : 25/09/2020 11:35 pm
(@questvba)
Posts: 125
Estimable Member
 

Hi Bob,
I opened your file in which there was already a macro. So I did the same thing (Mod_Sorting). I put the file with the macro.

What does the sent macro do? Here are the comments.

 

Option Explicit
Sub Sorting_()

Dim Plage As Range
Dim i As Byte
Dim LastColumn As Byte, LastRow As Long

'The With is used to specify the sheet on which processing is to be performed. This command ends with the End With.
'So, no surprise if you had selected another sheet.
With Sheets("Sheet1")
'LastColumn allows you to retrieve the last column of your table starting from E7 and going to the right.
LastColumn = .Range("F7").End(xlToRight).Column
'LastRow allows you to retrieve the last row of your table starting from E7 and going down
LastRow = .Range("F7").End(xlDown).Row
'Disabling the screen update for faster execution.
Application.ScreenUpdating = False
'I make a loop on all the columns of your table.
'I start at 5 because your table starts at column E.
For i = 5 To LastColumn
'In the loop, I select column after column.
Set Plage = .Range(Cells(7, i), Cells(LastRow, i))
'I do the sorting.
Plage.Sort Key1:=.Cells(7, i), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Next
'Enable screen update.
Application.ScreenUpdating = True

End With
End Sub

 

BR,

Lionel

 
Posted : 26/09/2020 4:36 am
(@knotbright)
Posts: 3
Active Member
Topic starter
 

Lionel, thank you very much, that's perfect 🙂

 
Posted : 26/09/2020 10:08 pm
Share: