How to sort Vba

EXCEL's standard functions can't solve some special sorting requirements. At this time, VBA can be used to correspond. For example, there is an original data table, as shown in figure:

According to these raw data, a classified summary data table is established, as shown in the following figure:

The requirement is that the summary table can be sorted automatically when the schedule data changes. Such requirements cannot be sorted automatically by hand. Such a requirement can only rely on VBA. First, compile a sorting program with the following code:

Subsort ()

Turn my worksheet into a string.

Dim arr( 1 to 3, 1 to 2)

I, j and k are represented as integers.

Dim M_S 1 As String' stores the name of the swap area.

Dim M_S2 is sold as a dual-store exchange.

MYsheet = "Summary ranking"

Worksheet (my worksheet). choose

Reads a value into an array.

For I = 1 to 3

For J = 1 to 2

arr(I,J) = Cells(I + 1,J + 1)

Next J.

Next, I

arrange

For I = 1 to 2

For J = I+1 to 3

If arr(I,2)& lt; Then arr(J, 2)

? M_S 1 = arr(I, 1)

? S2 = arr(I,2)

? arr(I, 1) = arr(J, 1)

? arr(I,2) = arr(J,2)

? arr(J, 1) = M_S 1

? arr(J,2) = M_S2

If ... it will be over.

Next J.

Next, I

Store the sorting result of the array in the cell.

For I = 1 to 3

Cell ((I+1), 2) = arr(I, 1)

Next, I

End joint

Then write an event trigger program for the change of cell contents. The code is as follows: (Note: this code should be placed in the original data table)

Private Worksheet _change(ByVal Target as Range)

If the target. Column < then = 4

? classify

If ... it will be over.

End joint