Enable autosort according to a column value for a range of cells.

Sometimes you need to autosort a certain range according to the values in a specific column, You can use the following VBA Macro in the specific excel sheet.


Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("C3:C20")) Is Nothing Then Exit Sub
Application.EnableEvents = False
Me.Sort.SortFields.Clear
Me.Sort.SortFields.Add Key:=Range("C3:C20"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Me.Sort
    .SetRange Range("A3:C20")
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Application.EnableEvents = True
End Sub

Values:
C3:C20  -> Column according to which the value will be sorted.
A3:C20 -> Range of cells which will be selected to sort according to the values in C3:C20 column.

No comments:

Post a Comment

Thank You.

https://linwintech.blogspot.com/