简体   繁体   English

Excel 使用 VBA 的动态多重自动过滤条件

[英]Dynamic multiple Autofilter Criteria for Excel using VBA

We have a tracking list with product IDs in Excel and we frequently have to compare entries for several products using this tracking list.我们有一个产品 ID 为 Excel 的跟踪列表,我们经常需要使用此跟踪列表比较多个产品的条目。 We use the .AutoFilter , search for the ID then click on "add to current selection".我们使用.AutoFilter ,搜索 ID,然后单击“添加到当前选择”。 We repeat that N times.我们重复 N 次。 I want to automate this using VBA.我想使用 VBA 自动执行此操作。

I have constructed an Input collector and as far as I can understand I need to collect the data in an Array.我构建了一个输入收集器,据我所知,我需要将数据收集到一个数组中。

Here a screenshot of a test worksheet.这是测试工作表的屏幕截图。

在此处输入图像描述

And here a test code that is gets an array and plugs it into .AutoFilter with 'xlFilterValues' this does yield not the desired outcome but rather and empty list.这里有一个测试代码获取一个数组并将其插入到带有“ .AutoFilter ”的 .AutoFilter 中,这不会产生所需的结果,而是产生空列表。

Sub Multifilter()

Dim FilteredRNG As Range
Dim TestAR(4) As Long
TestAR(0) = 100034
TestAR(1) = 165738
TestAR(2) = 165510
TestAR(3) = 165512
TestAR(4) = 165567

Set FilteredRNG = Sheet2.Range("B1:B29") ' Get my test range

FilteredRNG.AutoFilter Field:=1, Criteria1:=TestAR, Operator:=xlFilterValues
End Sub

Result is here:结果在这里: 在此处输入图像描述

If I switch the Operator to xlOr the Result changes to:如果我将运算符切换为xlOr ,结果将更改为:

在此处输入图像描述

My Array looks good during debugging:我的阵列在调试期间看起来不错: 在此处输入图像描述

So how do I get N IDs selected from the ID list using an array?那么如何使用数组从 ID 列表中选择 N 个 ID 呢?

I figured it out.我想到了。 The array needs to be a string in order for it to work.该数组需要是一个字符串才能工作。

Dim TestAR(4) As String 

Solves the issue.解决问题。

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

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