Sometime, you want to automatically sort the data in a range according to the values in a specific column where the data is dynamic (changes every time). The following code will auto-sort data in range A3:C20 according to the values in column C (C3:C20) when there is a change in the values in the column C (C3:C20):
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
No comments:
Post a Comment
Thank You.
https://linwintech.blogspot.com/