使用可变标准 VBA 进行自动过滤

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18106258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:21:41  来源:igfitidea点击:

Autofilter using variable Criteria VBA

excelvbaautofilter

提问by Kris Van den Bergh

I am trying to put an autofilter on a worksheet using a variable, but it is not working.

我正在尝试使用变量在工作表上放置自动过滤器,但它不起作用。

  Dim Criteria As Object
  i = 1
  Set Criteria = ActiveSheet.Cells(i, 1)
  MsgBox (Criteria.Value) ' this returns BC01.03

However, when I try to filter "Criteria.Value" is not returning anything.

但是,当我尝试过滤“Criteria.Value”时没有返回任何内容。

Selection.AutoFilter
ActiveSheet.Range("$A:$BM4").AutoFilter Field:=2, Criteria1:=" & Criteria.Value & "

Please advice.

请指教。

回答by David Zemens

Get rid of the quote marks around your Criteria1 argument:

去掉 Criteria1 参数周围的引号:

ActiveSheet.Range("$A:$BM4").AutoFilter Field:=2, Criteria1:=Criteria.Value

Also, just a question why you would use late binding on a Range object? I do use late binding for objects that would require another library/reference, but since Rangeis part of the Excel object library, I would use early binding. I would Dim Criteria as Rangebut that's just my preference.

另外,只是一个问题,为什么要在 Range 对象上使用后期绑定?对于需要另一个库/引用的对象,我确实使用后期绑定,但由于Range是 Excel 对象库的一部分,我将使用早期绑定。我愿意,Dim Criteria as Range但这只是我的偏好。