vba 更改过滤器的宏按钮

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

Macro button to change filter

excelvbaexcel-2007

提问by Ghokun

I want to have buttons to change my excel filter.

我想要按钮来更改我的 excel 过滤器。

I filter my fields with contains=somethingso each button should change that something text.

我过滤我的字段,contains=something所以每个按钮都应该改变一些文本。

Button1: change filter to contains=button1

Button1:将过滤器更改为 contains=button1

Button2: change filter to contains=button2

Button2:将过滤器更改为 contains=button2

and so on..

等等..

回答by Ghokun

Since I found answer myself, I put it here for future help.

由于我自己找到了答案,因此我将其放在这里以供将来帮助。

    Sub AI()

    '            sheet range and which field you want to filter and criteria to search
        ActiveSheet.Range("$A:$Z3").AutoFilter Field:=14, Criteria1:="stringtomatch"
    End Sub

回答by Aamir

You could make it easier by referencing the the filter search string from a cell as follows.

您可以通过引用单元格中的过滤器搜索字符串来简化操作,如下所示。

Sub Filter()
    Dim searchField As String

    searchField = "=*" & Range("H2") & "*"

    ActiveSheet.Range("$A:$H401").AutoFilter Field:=8, Criteria1:= _
        searchField, Operator:=xlAnd

End Sub

回答by Pawel

I would add a HLOOKUP in e.g. cell h2 that would be controlled by the spin button changing field F1. in this case we have a full solution

我会在例如由旋转按钮更改字段 F1 控制的单元格 h2 中添加 HLOOKUP。在这种情况下,我们有一个完整的解决方案

 =HLOOKUP(h2;h2:h100;F1;0)

This is combined with this macro provided above:

这与上面提供的这个宏相结合:

Sub Filter()
     Dim searchField As String

     searchField = "=*" & Range("H2") & "*"

    ActiveSheet.Range("$A:$H401").AutoFilter Field:=8, Criteria1:= _
        searchField, Operator:=xlAnd

 End Sub