简体   繁体   中英

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. 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). I also want to filter by the LOB array that contains the groups I'm trying to target.

When I set bikeARR = Tester.Range("A2:A" & lastRow) directly from the sheet the autofilter does not work. I've tried to format the values as 'TEXT' within 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. If I use bikeARR = Array("145", "147", "11868", "281") the TableauRCSA filters correctly. It will first filter for the 3 LOBs and then filter for each of the ID values. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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