Wednesday 22 December 2010

Automatically Sort Excel Range

Had another Excel request through – You wait a month for one then two come through at once.

This time the request is how to make a sheet update automatically when a certain cell is updated, This is how I did it (Although in all honesty there are hundreds of snippets on the net if you look for them.)

Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Worksheets(8).Range("N:N"), Target) Is Nothing) Then ‘Change this Range to contain the cells you want it to trigger for
AutoSort ‘This calls the sub procedure below which does the actual work
End If
End Sub

Private Sub DoSort()
Worksheets(8).Range("L2:O49").Sort Key1:=Worksheets(8).Range("L2"), Order1:=xlAscending, Header:=xlNo 'This line does the sort as requested in this case sorting the table L2:O49 by the field in column L2 – All of these calculations take affect on Worksheet 8.
End Sub

No comments:

Post a Comment