开发者

Excel VBA Filtering Issue

开发者 https://www.devze.com 2023-02-02 20:59 出处:网络
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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号