Forum Discussion
Automate sorting
- Oct 07, 2023
As I mentioned, it is very confusing if a row jumps away, possibly out of sight, the moment you edit an amount.
I'd sort the sheet:
- When you activate the sheet
- When you open the workbook.
Press Alt+F11 to activate the Visual Basic Editor.
Select Insert > Module.
Copy the following code into the module:
Sub SortSheet() Application.ScreenUpdating = False With Worksheets("Sheet1") .Range("A1").CurrentRegion.Sort Key1:=.Range("B1"), Order1:=xlDescending End With Application.ScreenUpdating = True End SubChange Sheet1 to the name of the sheet with your data.
Double-click the sheet under Microsoft Excel Objects in the Project Explorer pane on the left.
Copy the following code into the worksheet module:
Private Sub Worksheet_Activate() Call SortSheet End SubDouble-click ThisWorkbook in the Project Explorer pane.
Copy the following code into the ThisWorkbook module:
Private Sub Workbook_Open() Call SortSheet End SubSwitch back to Excel.
Save the workbook as a macro-enabled workbook (*.xlsm).
Make sure that you allow macros when you open the workbook.
As I mentioned, it is very confusing if a row jumps away, possibly out of sight, the moment you edit an amount.
I'd sort the sheet:
- When you activate the sheet
- When you open the workbook.
Press Alt+F11 to activate the Visual Basic Editor.
Select Insert > Module.
Copy the following code into the module:
Sub SortSheet()
Application.ScreenUpdating = False
With Worksheets("Sheet1")
.Range("A1").CurrentRegion.Sort Key1:=.Range("B1"), Order1:=xlDescending
End With
Application.ScreenUpdating = True
End Sub
Change Sheet1 to the name of the sheet with your data.
Double-click the sheet under Microsoft Excel Objects in the Project Explorer pane on the left.
Copy the following code into the worksheet module:
Private Sub Worksheet_Activate()
Call SortSheet
End Sub
Double-click ThisWorkbook in the Project Explorer pane.
Copy the following code into the ThisWorkbook module:
Private Sub Workbook_Open()
Call SortSheet
End Sub
Switch back to Excel.
Save the workbook as a macro-enabled workbook (*.xlsm).
Make sure that you allow macros when you open the workbook.