【问题标题】:Autofilter - dynamically linking the operator to a cell自动过滤器 - 将运算符动态链接到单元格
【发布时间】:2020-05-15 04:23:39
【问题描述】:

第一次发帖!大家好:)

希望对使用 VBA/Autofilter 有所帮助。我正在创建一个执行以下操作的筛选工具-

1) 用户使用下拉菜单和数字输入的组合在“ScreenerOptions”选项卡中输入一堆参数 2)“ScreenerOptions”中的选择驱动 VBA 代码自动过滤表,该表位于单独的选项卡“Master1”中

我希望将参数动态链接到单元格,因此在下拉列表中选择的任何选项都会驱动自动过滤器。除了运算符(例如 xlFilterValues、xlTop10Percent 等)之外,我已经得到了所有动态链接。

以下是我的筛选器中的部分参数。

这是有效的代码:

Sub Test()

With Sheets("Master1")

    'FCF Yield, row 14 in screener - if the user leaves the field blank
    If Worksheets("ScreenerOptions").Cells(14, 5) = "" Then

    'Then all rows in the table are displayed for this column
    'Column6 in the screener is pulling in the relevant
    'column number in the table based on MATCH formula
    Worksheets("Master1").Range("A4").AutoFilter Field:=Sheets("ScreenerOptions").Cells(14, 6)

    'If a parameter is entered, filter is applied
    'Criteria reference based on inputs in Screener Options Columns C (3) and E (5)

    Else

        'Formula in screener tool column 8 tells you if dropdown uses a value operator
        'e.g. ">", "=" returns 0
        'dropdowns containing text 'percent' return a 1

        'If 0 is returned, use operator xlOr to return numberical values and blanks
        If Worksheets("ScreenerOptions").Cells(14, 8).Value = 0 Then

        Worksheets("Master1").Range("A4").AutoFilter _
        Field:=Sheets("ScreenerOptions").Cells(14, 6), _
        Criteria1:=Sheets("ScreenerOptions").Cells(14, 3).Value & _
        Worksheets("ScreenerOptions").Cells(14, 5).Value, _
        Operator:=xlOr, Criteria2:=""

        Else
        'If 1 is returned, use operators xlTop10Percent, xlBottom10Percent as selected
        Worksheets("Master1").Range("A4").AutoFilter _
        Field:=Sheets("ScreenerOptions").Cells(14, 6), _
        Criteria1:=Worksheets("ScreenerOptions").Cells(14, 5).Value, _
        **Operator:=xlTop10Percent**
        End If

    End If

End With

Worksheets("Master1").Activate

End Sub

但是,当我将最后一个运算符更改为链接到单元格而不是硬编码 xlTop10Percent 时:

Operator:=Worksheets("ScreenerOptions").Cells(14, 3).Value

我得到错误:

Run-time '1004: Range 类的 Autofilter 方法失败。

有人可以帮忙吗?快完结了!

【问题讨论】:

    标签: excel vba autofilter


    【解决方案1】:

    字符串 "xlTop10Percent" 只是一个字符串 - VBA 中没有将其直接转换为 xlTop10Percent 常量值的机制。

    不过,您可以将其替换为数值 (5),这样应该可以。

    https://docs.microsoft.com/en-us/office/vba/api/excel.xlautofilteroperator

    如果您想在表中使用“友好”名称,则可以在工作表上创建一个查找表来处理数值的转换。

    【讨论】:

    • 谢谢!工作了一个款待。不敢相信我对数值一无所知!现在我可以安心睡觉了:)
    【解决方案2】:

    你的第一篇文章做得很好!

    关于您的代码,我不是专家,但我猜这是因为您分配了一个字符串值(如“dog”、“hello”或“xlTop10Percent”

    其实两行是一样的:

    Operator:=Worksheets("ScreenerOptions").Cells(14, 3).Value
    Operator:="xlTop10Percent"
    

    这完全不同:

    Operator:=xlTop10Percent
    

    运算符可以取值而不是文本,您可以尝试将单元格(14,3)设置为 5 而不是文本吗?

    应该可以,告诉我!

    保重!

    【讨论】:

    • 谢谢,有道理,而且数字很有效。祝你有个愉快的夜晚!
    猜你喜欢
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 2018-10-16
    • 2016-03-26
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多