简体   繁体   English

尝试使用“不等于”<>的自动过滤器中的标准变量,但无法使其正常工作

[英]Trying to use a variable for the criteria in autofilter with “not equal” <> and can not get it to work

I'm using Excel VBA to filter a list using the "not equals" expression.我正在使用 Excel VBA 使用“不等于”表达式过滤列表。 If I use Criteria1:="<>Bob" , the code runs perfectly, but if I change "Bob" to a variable, the code does not run.如果我使用Criteria1:="<>Bob" ,代码运行完美,但如果我将 "Bob" 更改为变量,则代码不会运行。

This works:这有效:

ActiveSheet.ListObjects("Rpt_AM_04_Sales_ShipTo").Range.AutoFilter Field:=4, _
    Criteria1:="<>Bob", Operator:=xlFilterValues

This does not work:这不起作用:

Dim Test_Criteria As String
Test_Criteria = "Bob"
ActiveSheet.ListObjects("Rpt_AM_04_Sales_ShipTo").Range.AutoFilter Field:=4, _
    Criteria1:<>Test_Criteria, Operator:=xlFilterValues

I expect the code to return a list that has everyone in it except Bob.我希望代码返回一个列表,其中包含除了 Bob 之外的所有人。 Instead, I get an error message just typing in the code:相反,我只需输入代码就会收到一条错误消息:

Compile error
Expected: named parameter

Incorrect syntax语法不正确

The code does not work because of incorrect syntax where you specify the criteria.由于您指定条件的incorrect syntax ,该代码不起作用。 Try this:尝试这个:

Change this:改变这个:

Criteria1:<>Test_Criteria

To this:对此:

Criteria1:"<>" & Test_Criteria

Complete code:完整代码:

Dim Test_Criteria As String
Test_Criteria = "Bob"
ActiveSheet.ListObjects("Rpt_AM_04_Sales_ShipTo").Range.AutoFilter Field:=4, _
     Criteria1:="<>" & Test_Criteria, Operator:=xlFilterValues

I hope this helps.我希望这有帮助。

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

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