简体   繁体   English

使用整数数组自动筛选多个条件

[英]Use array of integers to AutoFilter multiple criteria

I believe I'm having a data type issue.我相信我遇到了数据类型问题。 I am exporting a sheet as .csv from Tableau and trying to use a column of integers account IDs (that exports as 'General format) and enter those values into an array.我正在从 Tableau 将工作表导出为 .csv 并尝试使用一列整数帐户 ID(导出为“常规格式”)并将这些值输入到数组中。 I then want to use that array of integers within my autofilter so I can filter multiple criteria (there could be many integer IDs I might need to filter for, not just 2).然后我想在我的自动过滤器中使用该整数数组,以便我可以过滤多个条件(可能需要过滤许多整数 ID,而不仅仅是 2 个)。 I also want to filter by the LOB array that contains the groups I'm trying to target.我还想按包含我尝试定位的组的 LOB 数组进行过滤。

When I set bikeARR = Tester.Range("A2:A" & lastRow) directly from the sheet the autofilter does not work.当我直接从工作表中设置bikeARR = Tester.Range("A2:A" & lastRow) ,自动过滤器不起作用。 I've tried to format the values as 'TEXT' within Excel.我试图在 Excel 中将值格式化为“文本”。 I've also tried adding in the " ' " prior to the IDs to force it to show as text, but when I fill bikeARR directly from the Tester sheet my filter does not work.我还尝试在 ID 之前添加“ ' ”以强制其显示为文本,但是当我直接从Tester表填写bikeARR ,我的过滤器不起作用。 If I use bikeARR = Array("145", "147", "11868", "281") the TableauRCSA filters correctly.如果我使用bikeARR = Array("145", "147", "11868", "281") TableauRCSA过滤器正确。 It will first filter for the 3 LOBs and then filter for each of the ID values.它将首先过滤 3 个 LOB,然后过滤每个 ID 值。 I believe that the array has to be text values but how can I force that to happen when I'm filling my array?我相信数组必须是文本值,但是当我填充我的数组时,如何强制发生这种情况?

Sub testArray()
Dim bikeARR, LOB As Variant
Dim TableauRCSA, Tester As Worksheet
Dim lastRow As Integer


Set TableauRCSA = Workbooks("BPM_Sprint_SharePoint_Tracking.xlsm").Sheets("IPRC_Publish_Process_Model_+_RC")
Set Tester = Workbooks("BPM_Sprint_SharePoint_Tracking.xlsm").Sheets("Tester")
lastRow = Tester.Cells(Rows.Count, 1).End(xlUp).Row
'bikeARR = Tester.Range("A2:A" & lastRow)

bikeARR = Array("145", "147", "11868", "281")

LOB = Array("CB", "CRE", "CIB")

TableauRCSA.Activate
TableauRCSA.Range("A1").AutoFilter Field:=1, Criteria1:=LOB, Operator:=xlFilterValues 'Filter for LOB element
TableauRCSA.Range("B1").AutoFilter Field:=2, Criteria1:=bikeARR, Operator:=xlFilterValues 'Filter for BIKE IDs
End Sub

I was able to find a solution to my problem.我能够找到解决我的问题的方法。 Using the CStr function it will convert the integer to a string and work in the autofilter.使用CStr函数,它将整数转换为字符串并在自动过滤器中工作。

Sub testArray()
Dim bikeARR, LOB As Variant
Dim TableauRCSA, Tester As Worksheet
Dim lastRow, total As Integer

Set TableauRCSA = Workbooks("BPM_Sprint_SharePoint_Tracking.xlsm").Sheets("IPRC_Publish_Process_Model_+_RC")
Set Tester = Workbooks("BPM_Sprint_SharePoint_Tracking.xlsm").Sheets("Tester")
lastRow = Tester.Cells(Rows.Count, 1).End(xlUp).Row

'Fill Team List array
ReDim bikeARR(0 To (lastRow - 1))
i = 0
For x = 2 To lastRow 'NOTE: Team list starts at C2
    bikeARR(i) = CStr(Tester.Cells(x, 1).Value) 'convert integer to string
    i = i + 1
Next x

LOB = Array("Commercial Banking", "Commercial Real Estate", "Corporate Banking")


TableauRCSA.Activate
TableauRCSA.Range("A1").AutoFilter Field:=1, Criteria1:=LOB, Operator:=xlFilterValues 
'Filter for LOB element
TableauRCSA.Range("A1").AutoFilter Field:=2, Criteria1:=bikeARR, Operator:=xlFilterValues 'Filter for BIKE IDs
End Sub

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

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