I have some code below which is crashing.

I need to delete items in sheet 1 not shown in sheet 2 of a lareg spreasheet 35000 plus.

Can someone clean up my code so stops crashing and ist quicker. It seems to work for small number of rows, but not for large number of rows.


There must be something simple wrong. An explannation of the code would be great as well.

VB Code:
  1. Sub SelectedDesigns2()
  2.  
  3. Dim sheetcount As Integer
  4. Dim FileLength As Long
  5. Dim sheetname As String
  6.  
  7. sheetcount = Worksheets.Count
  8.  
  9. If sheetcount < 2 Then
  10. MsgBox ("Muppet! You do not have a selected designs sheet.")
  11. Else
  12.  
  13. 'Activate sheet
  14. Worksheets(2).Activate
  15.  
  16. sheetname = ActiveSheet.Name
  17.  
  18. 'Activate sheet
  19. Worksheets(1).Activate
  20.  
  21. 'Find how many codes there are
  22. FileLength = ActiveSheet.UsedRange.Rows.Count
  23.  
  24.     Range("B:B").Select
  25.     Application.CutCopyMode = False
  26.     Selection.Insert Shift:=xlToRight
  27.     Range("B2").Select
  28.     ActiveCell.FormulaR1C1 = _
  29.     "=IF(ISERROR(VLOOKUP(RC[-1],'" & sheetname & "'!C1:C1,1,FALSE)),""n"",""y"")"
  30.    
  31.     Selection.AutoFill Destination:=Range("B2:B" & FileLength)
  32. End If
  33.  
  34. Range("B:B").Select
  35. Selection.Cells.Copy
  36. Range("b2").PasteSpecial xlPasteValues
  37.  
  38.  
  39. Range("B2").Activate
  40.  
  41.  
  42.  
  43. For i = 2 To FileLength
  44. If ActiveCell.Text = "y" Then
  45. ActiveCell.Offset(1, 0).Activate
  46. Else
  47. ActiveCell.EntireRow.Delete
  48. End If
  49. Next i
  50.  
  51. Range("B:B").Delete
  52.  
  53.  
  54. End Sub

Boris