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.
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: