How do I fix this error: Object doesn't support this property or method (Error 438)

Copper Contributor

Here's the code:

 

Range("F5:H16").Select
Selection.ClearContents
ActiveWorkbook.Worksheets("Racer").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Racer").AutoFilter.Sort.SortFields.Add2 Key:=Range _
("E4:E16"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Racer").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("C5:C16").Select
Selection.ClearContents
Range("M13").Select
ActiveCell.FormulaR1C1 = ""
Range("O13").Select
ActiveCell.FormulaR1C1 = ""
Range("Q13").Select
ActiveCell.FormulaR1C1 = ""
Range("F5").Select

1 Reply

@JanRogers 

Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Racer")

' Clear contents of range F5:H16
ws.Range("F5:H16").ClearContents

' Clear any existing sorting fields
ws.AutoFilter.Sort.SortFields.Clear

' Add sorting field for column E
ws.AutoFilter.Sort.SortFields.Add2 Key:=ws.Range("E4:E16"), SortOn:=xlSortOnValues, _
    Order:=xlAscending, DataOption:=xlSortNormal

' Apply sorting
With ws.AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

' Clear contents of range C5:C16
ws.Range("C5:C16").ClearContents

' Clear contents of specific cells
ws.Range("M13").Value = ""
ws.Range("O13").Value = ""
ws.Range("Q13").Value = ""

' Select cell F5
ws.Range("F5").Select