vba 使用文本框作为用户输入的“包含”过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8960340/
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-11 15:00:09  来源:igfitidea点击:

'Contains' Filter with TextBox as User Input

vbaexcel-vbaexcel-2007excel

提问by User124726

I'm guessing this is simple but I'm struggling to find a proper solution to this.

我猜这很简单,但我正在努力寻找合适的解决方案。

I require to use the 'Contains' filter with a TextBox as the user input. Eg: User types in "Hello" in the TextBox and results returned are "Hello USA", "Hello Buddy", "Hello" etc.

我需要使用带有文本框的“包含”过滤器作为用户输入。例如:用户在文本框中输入“Hello”,返回的结果为“Hello USA”、“Hello Buddy”、“Hello”等。

The piece of code which I'm stuck with

我被困住的那段代码

 Selection.AutoFilter Field:=1, Criteria1:=UserForm1.TextBox1.Value, Operator:=xlOr

Right now it just gives me cells with the exact word in it. Could anyone point me in the right direction or a tutorial link.

现在它只给我包含确切单词的单元格。任何人都可以指出我正确的方向或教程链接。

Thanks for your time.

谢谢你的时间。

回答by GSerg

When in doubt, record a macro using the macro recorder. Which will give you:

如有疑问,请使用宏录制器录制宏。这会给你:

Selection.AutoFilter Field:=1, Criteria1:="=Hello*", Operator:=xlAnd

Therefore,

所以,

Selection.AutoFilter Field:=1, Criteria1:="=" & UserForm1.TextBox1.Value & "*", Operator:=xlAnd