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.
Dear Hans …
Below is two columns from the excel sheet I use :-
1 | rent | 1500 |
| 2 | loans | 659 |
| 3 | accounting | 400 |
| 4 | electrical bill | 300 |
| 5 | employment | 210 |
| 6 | visa payment | 120
|
| 7 | system maintenance | 50 |
| 8 | hospitality | 15 |
| 9 | packaging | 15 |
| 10 | cleaning | 13 |
lets say that I want to change electrical bill to 450 , visa payment to 625 , and loans to 275
What I am looking for is as soon as I enter the new number it sorts automatically to were it should be , if possible in any way possible
thanks in advance
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.