title: Application defined or object defined error at if statement
body: <p>I'm new to VBA and am trying to design a program that will go through a column with Strings in it and for every unique String name create a new worksheet object with that String value as its name and then copy and paste the values in that row to the new sheet. All identical Strings should then also have the values in their row copied over to the new sheet. The data is not sorted based on the Strings so I might have String a, String b, String a, in a column and I want both String a's to be a part of the same new sheet. Before I added a few lines of code to account for this everything was working fine, but now I'm getting an application defined or object defined error at an if statement that shouldn't be related to the added code. Here it is:</p>
  
  <pre><code>Sub FilterByClass()
  Dim i As Long
  Dim j As Long
  Dim sheetName As String
  Dim sheet As Worksheet
  Dim book As Workbook
  Dim k As Integer
  
  ActiveSheet.Name = "AllClasses"
  sheetName = Worksheets("AllClasses").Cells(2, 1).Value
  Worksheets.Add
  ActiveSheet.Name = sheetName
  Worksheets("AllClasses").Activate
  ActiveSheet.Rows("1:2").Copy
  Worksheets(sheetName).Paste
  j = 3
  k = 0
  For i = 3 To Rows.Count
      If Worksheets("AllClasses").Cells(i, 1).Value &lt;&gt; Worksheets("AllClasses").Cells(i - 1, 1).Value     Then        //site of error
          Worksheets("AllClasses").Range("1:1," &amp; j &amp; ":" &amp; (i - 1)).Copy
          Worksheets(Worksheets("AllClasses").Cells((i - 1), 1).Value).Paste
          j = i
          sheetName = Worksheets("AllClasses").Cells(i, 1).Value
          For Each sheet In ActiveWorkbook         //new added code block
              If sheetName = sheet.Name Then k = 1
              Next sheet
           If k = 1 Then k = 0
           Else
               Worksheets.Add
               ActiveSheet.Name = sheetName
               Worksheets("AllClasses").Activate
           End If
    Next i
    End Sub
  </code></pre>
  
  <p>Any help would be greatly appreciated.
  ¸</p>