Excel VBA Filtering Issue
Awesome Stackoverflow users,
I am having difficulty with something that seems simple enough to not cause this much trouble. I am trying to filter a given sheet based on a criteria. Here is the code:
Sub FilterWorksheet(sCriteria As String)
'First, clear the filter
ThisWorkbook.Worksheets("MyAwesomeSheet").AutoFilterMode = False
'Then apply the filter for the Transfer_From_seg column
ThisWorkbook.Worksheets("MyAwesomeSheet").AutoFilter Field:=2, Criteria1:=sCriteria
End Sub
For some reason, the last line where I actually apply the filter keeps giving me the following error message:
Runtime Error '448'
Named argument not found
Am I doing something really stupid with this?
Thanks!
Update:
So I found a solution. Here is what it looks like:
Sub FilterWorksheet(sCriteria As String)
'First, clear the filter
ThisWorkbook.Worksheets("MyAwesomeSheet").AutoFilterMode = False
'Then apply the filter for the Transfer_From_seg column
ThisWorkbook.Worksheets("MyAwesomeSheet").Range("A:H").AutoFilter Field:=2, Criteria1:=sCriteria
End Sub
I am not quite sure why this works & the previous one do开发者_如何学运维es not so if someone can explain it to me, that would be wonderful. Thanks again!
You need to specify the Range that you want to filter. For example:
ThisWorkbook.Worksheets("MyAwesomeSheet").Range("B3:C11").AutoFilter field:=2, Criteria1:=sCriteria
Autofilter Method in the Excel 2003 VBA Language Reference
精彩评论