I have 2 columns and 300k rows
F1 21
F2 32
F3 43
F4 4565
F5 76
F6 76
F7 5
F8 4
F9 4332
… …
The first column has 300 different values, and I want to delete all rows which contain specific values. Currently I have;
Sub DeleteRowsBasedOnCondition()
N = Cells(Rows.Count, 1).End(xlUp).Row
For i = N To 1 Step -1
If Cells(i, 1).Value = "F1" Then Rows(i).Delete
Next i
End Sub
and it removes a single conndition (F1), but this will take a long time to delete 56 conditions.
I have a named list, toRemove, which contains all the fields I want to remove. ie (F1, F2, F4, F9). I would like to refer to this named list instead of running the code 56 times.
I would like
Sub DeleteRowsBasedOnCondition()
N = Cells(Rows.Count, 1).End(xlUp).Row
For i = N To 1 Step -1
If Cells(i, 1).Value has a value in the array toRemove
Then Rows(i).Delete
Next i
End Sub
I tried How to search for string in an array post, but get the following error when referring to the line
function Filter(arr, stringToBeFound):
Run-Time error '13':
Type mismatch
Here is the code I tried:
Sub DeleteRowsBasedOnArrayCondition()
' toremove is a named list of values which are to be compared with and deleted from table
N = Cells(Rows.Count, 1).End(xlUp).Row
For i = N To 1 Step -1
If IsInArray(Cells(i, 2).Value, toremove) Then Rows(i).Delete
Next i
End Sub
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function