Auto sorting rows

Chronepsis

New Member
Joined
Jan 21, 2015
Messages
1
Hi. I am trying to write a macro abot autosort. But my macro is not working.
Preview of table and the macro i tried is attached below.

Firstly value on column D will be entered, after entering value on column E it will sum D and E to F and autosort(descending order) by the value calculated on column F.

Thx alot,

Edit: This macro ignores the sum row and sorts it too.

Code:
[TABLE="class: grid, width: 551, align: center"]
<tbody>[TR]
[TD]
[CENTER][B]A[/B][/CENTER]
[/TD]
[TD]
[CENTER][B]B[/B][/CENTER]
[/TD]
[TD]
[CENTER][B]C[/B][/CENTER]
[/TD]
[TD]
[CENTER][B]D[/B][/CENTER]
[/TD]
[TD]
[CENTER][B]E[/B][/CENTER]
[/TD]
[TD]
[CENTER][B]SUM[/B][/CENTER]
[/TD]
[/TR]
[TR]
[TD]
[CENTER]1[/CENTER]
[/TD]
[TD]
[CENTER]1[/CENTER]
[/TD]
[TD]
[CENTER]1[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]2[/CENTER]
[/TD]
[TD]
[CENTER]2[/CENTER]
[/TD]
[TD]
[CENTER]2[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]3[/CENTER]
[/TD]
[TD]
[CENTER]3[/CENTER]
[/TD]
[TD]
[CENTER]3[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]4[/CENTER]
[/TD]
[TD]
[CENTER]4[/CENTER]
[/TD]
[TD]
[CENTER]4[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]5[/CENTER]
[/TD]
[TD]
[CENTER]5[/CENTER]
[/TD]
[TD]
[CENTER]5[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]6[/CENTER]
[/TD]
[TD]
[CENTER]6[/CENTER]
[/TD]
[TD]
[CENTER]6[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]7[/CENTER]
[/TD]
[TD]
[CENTER]7[/CENTER]
[/TD]
[TD]
[CENTER]7[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]8[/CENTER]
[/TD]
[TD]
[CENTER]8[/CENTER]
[/TD]
[TD]
[CENTER]8[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]9[/CENTER]
[/TD]
[TD]
[CENTER]9[/CENTER]
[/TD]
[TD]
[CENTER]9[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]10[/CENTER]
[/TD]
[TD]
[CENTER]10[/CENTER]
[/TD]
[TD]
[CENTER]10[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]11[/CENTER]
[/TD]
[TD]
[CENTER]11[/CENTER]
[/TD]
[TD]
[CENTER]11[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]12[/CENTER]
[/TD]
[TD]
[CENTER]12[/CENTER]
[/TD]
[TD]
[CENTER]12[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]13[/CENTER]
[/TD]
[TD]
[CENTER]13[/CENTER]
[/TD]
[TD]
[CENTER]13[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]14[/CENTER]
[/TD]
[TD]
[CENTER]14[/CENTER]
[/TD]
[TD]
[CENTER]14[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]15[/CENTER]
[/TD]
[TD]
[CENTER]15[/CENTER]
[/TD]
[TD]
[CENTER]15[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]16[/CENTER]
[/TD]
[TD]
[CENTER]16[/CENTER]
[/TD]
[TD]
[CENTER]16[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]17[/CENTER]
[/TD]
[TD]
[CENTER]17[/CENTER]
[/TD]
[TD]
[CENTER]17[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]18[/CENTER]
[/TD]
[TD]
[CENTER]18[/CENTER]
[/TD]
[TD]
[CENTER]18[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]19[/CENTER]
[/TD]
[TD]
[CENTER]19[/CENTER]
[/TD]
[TD]
[CENTER]19[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]20[/CENTER]
[/TD]
[TD]
[CENTER]20[/CENTER]
[/TD]
[TD]
[CENTER]20[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]21[/CENTER]
[/TD]
[TD]
[CENTER]21[/CENTER]
[/TD]
[TD]
[CENTER]21[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]22[/CENTER]
[/TD]
[TD]
[CENTER]22[/CENTER]
[/TD]
[TD]
[CENTER]22[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD]
[CENTER]23[/CENTER]
[/TD]
[TD]
[CENTER]23[/CENTER]
[/TD]
[TD]
[CENTER]23[/CENTER]
[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="colspan: 3"]
[CENTER][B]SUM[/B][/CENTER]
[/TD]
[TD]
[CENTER][B]0[/B][/CENTER]
[/TD]
[TD="align: center"][B]0[/B]
[/TD]
[TD="align: center"][B]0[/B]
[/TD]
[/TR]
</tbody>[/TABLE]


Private Sub Worksheet_Change(ByVal Target As Range)
    Dim intLR As Integer
    intLR = Cells.SpecialCells(xlLastCell).Row
    If Target.Column = 6 Then 'column A - date
        'Sort range
        Range("A1:F" & intLR).Select
        ActiveWorkbook.Worksheets("TEST").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("TEST").Sort.SortFields.Add Key:=Range("F1:F" & intLR), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("TEST").Sort
            .SetRange Range("A1:F" & intLR)
            .Header = xlYes
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("A2").End(xlDown).Offset(1, 0).Select
    End If
End Sub
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top