Hi guys! Trying to implement a VBA loop the first time and struggling with it. I have table Table1; separately defined range Filtered (same area as Table1, however no headers) and hardcoded array of values Arr. I want VBA to cycle through all those values in array and use each of them to filter the 1st column of Table1. If filtering provides results (visible rows - other than table header), I want to run a separate code, but if there is no match and the range is filtered blank, I want to just go to next value in array and continue with the loop.
I was testing with a simple nested code that fills in A2 values top to bottom on every positive match for testing purposes. I see how VBA throttles through the filtering with the values in array, however I don't get expected results from the nested code, so I assume the IF clause is not working properly.
- this provides a positive response on all runs, even if all values from array are not available in table.
- this would provide all negative runs
I have designed array values and table contents to only partly match.
What could I be missing here? Thanks a lot for any input?
I was testing with a simple nested code that fills in A2 values top to bottom on every positive match for testing purposes. I see how VBA throttles through the filtering with the values in array, however I don't get expected results from the nested code, so I assume the IF clause is not working properly.
Code:
If Filtered.Rows.Count > 1 And Not Filtered Is Nothing Then
Code:
If Filtered.Rows.Count > 0 And Not Filtered Is Nothing Then
I have designed array values and table contents to only partly match.
Code:
Sub Filter_item()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim Filtered As Range, i As Long, Arr
Arr = Array("Apple", "Orange", "Grape")
For i = LBound(Arr) To UBound(Arr)
With ws.ListObjects("Table1").Range
.AutoFilter Field:=1, Criteria1:=Arr
Set Filtered = .SpecialCells(xlCellTypeVisible)
End With
If Filtered.Rows.Count > 1 And Not Filtered Is Nothing Then
For Each cell In Range("Testrange").Cells
If IsEmpty(cell) = True Then cell.Select: Exit For
Next cell
ActiveCell = Range("A2").Value
End If
Set Filtered = Nothing
Next i
End Sub
What could I be missing here? Thanks a lot for any input?