0

We have a tracking list with product IDs in Excel and we frequently have to compare entries for several products using this tracking list. We use the .AutoFilter, search for the ID then click on "add to current selection". We repeat that N times. I want to automate this using 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.

enter image description here

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.

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: enter image description here

If I switch the Operator to xlOr the Result changes to:

enter image description here

My Array looks good during debugging: enter image description here

So how do I get N IDs selected from the ID list using an array?

4
  • You need to use Operator:=xlFilterValues to filter by an array of values. • Also you should remove the parenthesis from GetArray (varArray) and GetFilteredRNG (FilteredRNG) as they do something different than you think they do. Use GetArray varArray or put a Call statement infront Call GetArray(varArray). As you have them now they will convert the default ByRef parameter into a ByVal submission. You can see that because there is a space between the function name GetArray and the parenthesis. If that's the case it is a conversion to ByVal and the parenthesis needs removed. Commented Mar 24, 2022 at 7:11
  • Operator:= xlFiltervalues doesn't work either. FYI: the GerArray and GetFilterRNG are dummy subs that I use to make this code StackOverFlow readable. In my code I have it all spaghettied, but thanks for the info I didn't know that. Commented Mar 24, 2022 at 7:33
  • 2
    Well then please show a minimal reproducible example of your issue. Because with an proper array and Operator:=xlFilterValues it works. Give some example data for your array and some example data you try to filter. Commented Mar 24, 2022 at 8:19
  • Give me a minute, I'll update the post with some test data and then it should be more clear. Commented Mar 24, 2022 at 12:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.