What I need to do is filter through a table by using the second column from each row from another table. The table that I loop through contains 11 rows and the code loops 11 times, but it uses the 11th row each time. What it needs to do is go from the first row to the last row.
Edit: With help of Variatus I have applied a count
instead of For Each
. This seems to be able to go through all the rows, except the last row. To be continued.
Edit2: for those who are curious, have a look at my follow up question where I got everything to work! Filter a table with an array of criteria derived from another table
Sub LoopDoorAfdelingV4()
Dim myTable As ListObject
Dim myTable2 As ListObject
Dim oRow As ListRow
Dim c As Long
Dim myGroupIDFilter As Variant
Dim myGroupNameFilter As Variant
Set myTable = ActiveSheet.ListObjects("TabelGroupID")
Set myGroupIDFilter = myTable.ListColumns(1).Range
Set myGroupNameFilter = myTable.ListColumns(2).Range
Set myTable2 = ActiveSheet.ListObjects("TabelAfdelingenIntern")
For c = 1 To myTable.ListRows.Count
ActiveSheet.Range(myTable2).AutoFilter Field:=1, Criteria1:=myGroupNameFilter(c), _
Operator:=xlOr
Next c
End Sub
Consider setting up the filtering code with the maximum number of criteria you wish to be able to handle, like this (here demonstrated with just 3),
Set the UBound for
myGroupNameFilter
to that same maximum number. Use the loop reading the criteria to fill the myGroupNameFilter array. Fill the elements of myGroupNameFilter for which you have no values with random values which you know can't exist in the list to be filtered, perhaps "XYZ987" or -99999. The filter should return the specified result because those criteria which find no match will not influence the result.I hate to publish this code because it is sure not to work and I have no way of testing. It is intended to merely show the idea.
The point is that you can't set the filter in the loop in which you set the filter criteria.