Active Member
September 24, 2020
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.
September 9, 2020
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
September 9, 2020
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
1 Guest(s)