简体   繁体   English

使用变量的多标准VBA自动过滤器

[英]VBA Autofilter With Multiple Criteria Using Variables

EDIT: To fix the issue, I changed Dim Placed As Range to As Long . 编辑:为了解决这个问题,我将Dim Placed As Range更改为As Long I then changed 然后我改变了

Set Placed = Rows("3:3").Find("Placed", Range("A3"), searchdirection:=xlToRight)

to

Placed = Rows("3:3").Find("Placed", Range("A3"), searchdirection:=xlToRight).Column

The code works just fine now. 代码现在运行得很好。 END EDIT 结束编辑

Ok, I've been working on this issue for almost two hours now. 好的,我已经在这个问题上工作了近两个小时了。

I'm trying to code a couple of option buttons on a worksheet to filter the data as needed. 我正在尝试在工作表上编写几个选项按钮来根据需要过滤数据。

To begin, I recorded myself filtering the data to give me a starting point. 首先,我记录了自己过滤数据给我一个起点。 This is what the recorder spit out: 这是录音机吐出的内容:

ActiveSheet.Range("$A$3:$CS$212").AutoFilter Field:=53, Criteria1:=Array( _
    "Iteration 1", "Iteration 2", "Iteration 3", "Tradeshow", "="), Operator:= _
    xlFilterValues

To make the option buttons more robust, I decided to use variables just in case columns or rows were added, or if criteria were added. 为了使选项按钮更加健壮,我决定使用变量,以防添加了列或行,或者添加了标准。

I added a variable for the Range() , Field:= , and Criteria1:= , but my code throws this error now: Run-time error '1004': Autofilter Method of Range class failed . 我为Range()Field:=Criteria1:=添加了一个变量,但是我的代码现在抛出了这个错误: Run-time error '1004': Autofilter Method of Range class failed

I'm wondering if I'm using the Array improperly...? 我想知道我是否使用不正确的Array ...? Anyway, here are my declarations: 无论如何,这是我的声明:

Const Opt1 As String = "Iteration 1"
Const Opt2 As String = "Iteration 2"
Const Opt3 As String = "Iteration 3"
Const Opt4 As String = "Iteration 4"
Const Opt5 As String = "Tradeshow"
Const Opt6 As String = "Placed"

Dim Placed As Range             'This is the Field var.
Dim lastRow, lastColumn As Long 'Holds the last row and column numbers.
Dim Rng1, Rng2 As Range         'These hold the beginning and ending ranges for the filter

And here's how I'm setting my variables: 以下是我设置变量的方法:

lastRow = Range("A:A").Find("*", Range("A1"), searchdirection:=xlPrevious).Row
lastColumn = Cells(3, Columns.Count).End(xlToLeft).Column
Set Placed = Rows("3:3").Find("Placed", Range("A3"), searchdirection:=xlToRight)
Set Rng1 = Cells(3, 1)
Set Rng2 = Cells(lastRow, lastColumn)

Finally, here is the AutoFilter code: 最后,这是AutoFilter代码:

ActiveSheet.Range(Rng1, Rng2).AutoFilter Field:=Placed, Criteria1:=Array(Opt1, Opt2, Opt3, Opt4, Opt5, Opt6, "="), Operator:=xlFilterValues

Does anyone see why it might be throwing that error? 有谁知道为什么它会抛出那个错误? Does it have something to do with the Array ? 它与Array吗? Any help will be much appreciated! 任何帮助都感激不尽!

No, it's an issue with the Field:= parameter. 不,这是Field:=参数的问题。 You have it set to Placed, and you have assigned the Placed variable to a cell. 您已将其设置为“已放置”,并且已将“放置”变量分配给单元格。 It needs to be a column number. 它必须是列号。 For example, if the Placed column is column D and your data starts in column A, the Field should be 4, because it is the fourth column. 例如,如果Placed列是D列而您的数据从A列开始,则Field应为4,因为它是第四列。 If the placed column is the last column, you could set Field equal to your lastColumn variable: 如果放置的列是最后一列,则可以将Field设置为等于lastColumn变量:

ActiveSheet.Range(Rng1, Rng2).AutoFilter Field:=lastColumn, Criteria1:=Array(Opt1, Opt2, Opt3, Opt4, Opt5, Opt6, "="), Operator:=xlFilterValues

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

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