title: Filtering out multiple items
body: <p>I'm trying to filter out a large number of items from a list. I have about 500 keys to filter out of an input list of 700. I don't know if there's a list item for each key though, so the "dumb" approach I went down was to loop through each key comparing it to each row of the input list. The code I've written technically works, but it's not efficient and crashes as soon as I try testing with too many keys. Is there a more elegant solution?</p>
  
  <p>My attempt:</p>
  
  <pre><code>Sub ExcludeKeys()
  
  'Copy input to new output sheet
  Dim outputTabName As String
  Worksheets("Input").Copy After:=Worksheets(Worksheets.Count)
  outputTabName = "Output " + Strings.Trim(Worksheets.Count - 3)
  Worksheets(Worksheets.Count).Name = outputTabName
  
  'Get the column letter the keys are in (input data)
  Dim keyCol As String
  keyCol = Strings.Trim(Worksheets("Home").Range("D17").Value)
  
  Dim row As Range
  Dim totalRows As Long
  Dim keyToEval As Variant
  
  'Make an array out of existing keys
  totalKeys = Worksheets("Keys").Rows.End(xlUp).row
  
  'Go through the Output sheet
  For rowNum = 2 To Worksheets(outputTabName).Rows.Count
  
      'Go through all keys to exclude
      For i = 1 To LastRow(Worksheets("Keys").Name, 1)
  
          'Get new key to compare to (in output)
          keyToEval = Strings.Trim(Worksheets(outputTabName).Range(keyCol &amp; rowNum).Value)
  
          'If keys match, delete
          If keyToEval = Strings.Trim(Worksheets("Keys").Range("A" &amp; i).Value) Then
              Worksheets(outputTabName).Range(keyCol &amp; rowNum).Rows.EntireRow.Delete
              'If a row was deleted, go back 1 (avoids skipping next)
              i = 1
          End If
      Next i
  Next rowNum
  
  End Sub
  
  '----------
  ' Handy function to find the last row of a column
  ' Source: https://stackoverflow.com/a/48504852/4984516
  Function LastRow(Optional strSheet As String, Optional columnToCheck As Long = 1) As Long
  
      Dim shSheet As Worksheet
  
      If strSheet = vbNullString Then
         Set shSheet = ActiveSheet
      Else
          Set shSheet = Worksheets(strSheet)
      End If
  
      LastRow = shSheet.Cells(shSheet.Rows.Count, columnToCheck).End(xlUp).row
  
  End Function
  </code></pre>