Reputation: 189
I am trying to sort data through excel VBA but it is already sorted by a different header. How would I clear all the sort fields before I set the new one? I have tried Sort.SortFields.Clear
but it hasn't worked. Any help would be greatly appreciated. Here's the code I'm using:
Sheet9.Activate
Columns("J:J").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(1, "J").Value = "InternalID"
Set rng2 = FindHeader("CLIENT NAME", Sheet9.Name)
Count = 1
Sheet9.Cells.Select
With Sheet9.Sort
.SortFields.Clear
.SortFields.Add Key:=rng2, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Upvotes: 0
Views: 27814
Reputation: 1
ActiveSheet.Range("Your_Range_Value").AutoFilter Field:=1
I did this for every column that has a sort field and it clears them out. Where Field:=X is the column number in your range that you want to clear.
This is what I have in my Workbook. Sheet1 is code name of sheet, and rng is string representing the range value ("B4:G1500")
' Clear all filters
Sheet1.Range(rng).AutoFilter Field:=1
Sheet1.Range(rng).AutoFilter Field:=2
Sheet1.Range(rng).AutoFilter Field:=3
Sheet1.Range(rng).AutoFilter Field:=4
Sheet1.Range(rng).AutoFilter Field:=5
Sheet1.Range(rng).AutoFilter Field:=6
Upvotes: 0