Excel VBA过滤问题

时间:2011-01-05 15:23:15

标签: excel-vba excel-2007 autofilter vba excel

令人敬畏的Stackoverflow用户,

我遇到一些似乎很简单但不会造成这么多麻烦的事情。我试图根据标准过滤给定的表。这是代码:

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

由于某种原因,我实际应用过滤器的最后一行不断给出以下错误消息:

Runtime Error '448'

Named argument not found

我做了一件非常愚蠢的事吗?

谢谢!

更新

所以我找到了解决方案。这是它的样子:

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

我不太确定为什么会这样做&如果有人可以向我解释,前一个不是这样,那将是美好的。再次感谢!

1 个答案:

答案 0 :(得分:1)

您需要指定要过滤的范围。例如:

ThisWorkbook.Worksheets("MyAwesomeSheet").Range("B3:C11").AutoFilter field:=2, Criteria1:=sCriteria

Autofilter Method in the Excel 2003 VBA Language Reference