Automatically leave dates in order

Asked

Viewed 2,650 times

1

How do I leave my table that has dates organized in order, type, has the cells: date of issue; name; expiration date; situation, I wanted my table organized by date of expiration automatically when I put a new value, either by pressing a button or when I open it. I believe I should use vba, only I do not understand anything msm.

DSD thanks anyone who can help.

1 answer

0


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:

inserir a descrição da imagem aqui

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)

inserir a descrição da imagem aqui

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

inserir a descrição da imagem aqui

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

inserir a descrição da imagem aqui

3.3) Draw the button on the worksheet, select the macro OrdernarPlanilha1 and click OK

inserir a descrição da imagem aqui

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

inserir a descrição da imagem aqui

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

  • 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

  • I added a solution at the end of the answer to solve the problem of several tables in the same spreadsheet.

  • 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 example Range("A1:C5").Sort Key1:=Range("B1")...

  • Man, thank you so much, you figured it out here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.