0

I am trying to filter a table based of 2 criteria.

Every time my macro runs, it displays the

Run-time Error '5'. Invalid procedure call or argument.

message once it gets to filtering the sheet.

I've switched laptops and copied the macro word by word and saved it in my personal macro book. With my previous laptop, this macro ran without error (my previous laptop was a Lenovo and this one is a Dell).

vba code

Range("A2:X2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Range("A:X").AutoFilter Field:=16, Criteria1:="<>*East*", Criteria2:="<>*VO*", Operator:= xlFilterValues
Selection.ClearContents
ActiveSheet.Range("A:X").AutoFilter Field:=16, Criteria1:="PR VO"
ActiveSheet.Range("A:X").AutoFilter Field:=11, Criteria1:="<>*EC *", Operator:= xlFilterValues
Selection.ClearContents
ActiveSheet.Range("A:X").AutoFilter Field:=11
ActiveSheet.Range("A:X").AutoFilter Field:=16
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range ("A:A"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
   .Header = xlYes
   .MatchCase = False
   .Orientation = xlTopToBottom
   .SortMethod = xlPinYin
    .Apply
End With

I expect this code to filter the values that do not match the criteria and clear that data. This is basically to filter out candidates for our Eastern Cape Region. The result however, is Run-time Error '5'. Invalid procedure call or argument.

Pawel Czyz
  • 1,651
  • 4
  • 17
  • 21
SiyaG
  • 1
  • 2
    Please read [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) – Tim Stack Jun 05 '19 at 12:04
  • Also consider using explicit references to `Worksheets` and `Workbooks` instead of `ActiveSheet` et al which make the code unreadable and unreliable. – Stavros Jon Jun 05 '19 at 12:59

0 Answers0