【发布时间】: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