简体   繁体   English

VBA根据数字数组自动筛选多个工作表,如果不在列表中,则忽略

[英]VBA AutoFilter multiple worksheets based on array of numbers, ignore if not in list

Let's say I have a workbook with 3 worksheets on it. 假设我有一本包含3个工作表的工作簿。 I've filtered down the first sheet based on column State that equals Ohio . 我根据等于俄亥俄州的 State过滤了第一张表。 I then get a list of values from that filtered list from column ID and put them into an Integer/Long array. 然后,我从列ID的筛选列表中获取值列表,并将它们放入Integer / Long数组中。 Basically I put all the ID's for each row that has the state column Ohio . 基本上,我将状态列Ohio的每一行的所有ID都放入。

I am now trying to filter the remaining tables based on their ID column where they match my array of ID's . 现在我想筛选基于其ID列在那里我的ID阵列匹配其余的表。 The problem is when it comes across filtered ID's that are not in the new sheet, instead of providing me with the matches for each sheet, I am left with only the last matching value. 问题是,当遇到未包含在新工作表中的已过滤ID时 ,我没有为我提供每张工作表的匹配项,而是只剩下最后一个匹配值。 Here's an example of my VBA trying to filter the remaining sheets: 这是我的VBA尝试过滤剩余工作表的示例:

For Each sheet In Worksheets

If sheet.Name <> "Sheet1" Then
    sheet.Activate        

    columnNum = ActiveSheet.UsedRange.Find("ID").Column
    ActiveSheet.AutoFilterMode = False
    ActiveSheet.UsedRange.AutoFilter

    '***Problem is here
    ActiveSheet.UsedRange.AutoFilter Field:=columnNum, Criteria1:=mainIDs, Operator:=xlFilterValues

End If

Next

As I mentioned earlier mainIDs is an array of Integer that is being carried over to each sheet to filter on. 正如我前面提到的, mainIDs是一个整数数组,该整数被带到每个工作表上进行过滤。 Why is it only bringing me back the last match? 为什么只带我回到最后一场比赛? Do I need to somehow tell it to ignore filtered values that aren't in the new sheet? 我是否需要以某种方式告诉它忽略新表中没有的过滤值? How? 怎么样?

EDIT: Here's how I filled my array 编辑:这就是我填充数组的方式

Dim mainIDs() As Long, size As Integer, i As Integer

size = ActiveSheet.UsedRange.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 2
ReDim mainIDs(size)

i = 0
For Each row In .UsedRange.Columns(1).SpecialCells(xlCellTypeVisible)
    If row.Value <> "ID" Then '***Column header
        mainIDs(i) = row.Value
        i = i + 1
    End If
Next

If you use an array for filtering you have to cast the values to strings before. 如果使用数组进行过滤,则必须先将值转换为字符串。 As I understood you're adding integer/long values to the array. 据我了解,您正在向数组添加整数/长整数值。 Autofilter can't work with this. 自动筛选器无法与此配合使用。

In addition you have to change your array to a string array mainIDs() as String or declare it as Variant. 另外,您必须将数组更改为字符串数组mainIDs() as String或将其声明为Variant。

For Each row In .UsedRange.Columns(1).SpecialCells(xlCellTypeVisible)
    If row.Value <> "ID" Then '***Column header
        mainIDs(i) = Cstr(row.Value) 'Cast values to string
        i = i + 1
    End If
Next

Then it should work, please let me know if it helped you. 然后它应该起作用,请告诉我它是否对您有帮助。

If you use arrays as Criteria , you'll have to use a 1D array. 如果将数组用作Criteria ,则必须使用一维数组。
But then, you need to supply the Operator argument to actually filter all that is in the array. 但是,然后,您需要提供Operator参数来实际过滤数组中的所有内容。

Sample: 样品:

ActiveSheet.UsedRange.AutoFilter Field:=columnNum, _
                                 Criteria1:=mainIDs, _
                                 Operator:xlFilterValues

To know more about AutoFilter Method check MSDN . 要了解有关自动筛选方法的更多信息,请检查MSDN

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

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