Automatically sort using VBA
To implement the solution, it is necessary that the guide Desenvolvedor
is enabled. If you haven’t done this yet, see how to Show the Developer tab.
Taking as an example the following table:
Perform the following steps to automate sorting by the date column:
1) Create table sort macro
1.1) Click on the tab Desenvolvedor
1.2) Click the Visual Basic button (or press Alt
+F11
) to open the VBA window
1.3) In the menu Inserir
, click on the option Módulo
to add the Modulo1
to Vbaproject
1.4) In the code window of Modulo1
, add the following procedure:
Public Sub OrdernarPlanilha1()
Const INI_COL_ORDENAR = "Plan1!B1"
Range(INI_COL_ORDENAR).Sort Key1:=Range(INI_COL_ORDENAR), _
Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom
End Sub
This procedure will sort the entire table around the cell specified in the constant INI_COL_ORDENAR
.
The constant INI_COL_ORDENAR
should contain the address of the cell where the column containing the sorting values starts (in our example, it is the cell Plan1!B1
)
2) Calling sort macro when opening the spreadsheet file
2.1) In the module code window EstaPasta_de_trabalho
, add the following procedure:
Private Sub Workbook_Open()
OrdernarPlanilha1
End Sub
- This procedure automatically executes the sort when the file is opened
3) Add a button to call the sort macro
3.1) Go back to the Excel window and click on the tab Desenvolvedor
3.2) Click on the button Inserir Controles
and select the control Botão
3.3) Draw the button on the worksheet, select the macro OrdernarPlanilha1
and click OK
3.4) Change the button label to "Sort"
4) Call the sort macro when the spreadsheet is changed
Particularly, I don’t like this procedure because it changes the order of the lines while you edit the table and this can cause some confusion. But if you want to implement, just do the following:
4.1) In the VBA window, add the following code to the worksheet module (in the case of our example will be the module Plan1
)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
OrdernarPlanilha1
End Sub
All right, just use!
More than one table in the same spreadsheet...
If the worksheet has more than one table to be ordered, the sorting routine can be changed as suggested below to be executed for each of the tables:
Public Sub OrdernarPlanilha1()
OrdernarTabela "Plan1!B1"
OrdernarTabela "Plan1!B10"
End Sub
Private Sub OrdernarTabela(ini_col_ordenar As String)
Range(ini_col_ordenar).Sort Key1:=Range(ini_col_ordenar), _
Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom
End Sub
Dude. thank you very much, this already helps me a lot, however in the spreadsheet has several tables, separate and all of them need to be organized by date, has 4 accounts for example in the same spreadsheet, each account has name, date, value etc, so wanted q each of them if adjusted, this way you answered me she adjusts the entire spreadsheet according to the same column, are different tables in the same spreadsheet, if you have as I thank you very much
– user98257
How do I organize it from a specific column, when I put for example D4 in the variable la, it keeps organizing the whole table, and something else, in the table, has a column that cannot change, each table has 9 columns, however the column 9 n can go out of order, How do I do that? Thanks in advance
– user98257
I added a solution at the end of the answer to solve the problem of several tables in the same spreadsheet.
– Fernando
I cannot see a simple way to sort from a specific column, or not to change a column. To do this, you will need to specify the range of each table in the function
sort
. For exampleRange("A1:C5").Sort Key1:=Range("B1")...
– Fernando
Man, thank you so much, you figured it out here
– user98257