简体   繁体   English

Excel VBA筛选问题

[英]Excel VBA Filtering Issue

Awesome Stackoverflow users, 很棒的Stackoverflow用户,

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 does 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 Excel 2003 VBA语言参考中的自动筛选方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM