title: Why is referencing sheet by index still implicity referencing referencing by activesheet
body: <p>I Have the below code which formats a sheet. There are multiple sheets in this workbook so i only want to take actions on the first one. However if i have any sheet other than the first activated such as the second the code throws a 1004 error. I am not sure why as i am specifying and defining the target worksheet.</p>
  <pre><code>Option Explicit
  
  Sub SelectByJobNumber()
      Dim LastRow As Long
      Dim OperatingRow As Variant
      Dim CountOfMatching As Long
      
      Dim WB As Worksheet
      Set WB = ThisWorkbook.Worksheets(1)
      
      LastRow = WB.Cells(ActiveSheet.Rows.Count, &quot;C&quot;).End(xlUp).Row
      
          WB.Range(Cells(LastRow + 2, 19), Cells(LastRow + 2, 22)).Formula = &quot;=&quot; &amp; &quot;Sum(S74:S&quot; &amp; LastRow &amp; &quot;)&quot;
          WB.Range(Cells(LastRow + 2, 19), Cells(LastRow + 2, 22)).NumberFormat = &quot;$#,##0.00&quot;
      
      For OperatingRow = 74 To LastRow
          
          If WB.Cells(OperatingRow, 3) = WB.Cells(OperatingRow + 1, 3) Then
  
              CountOfMatching = CountOfMatching + 1
  
          Else
              
                  WB.Range(Cells(OperatingRow - CountOfMatching, 3), Cells(OperatingRow, 21)).BorderAround ColorIndex:=1, Weight:=xlMedium
                  WB.Cells(OperatingRow - CountOfMatching, 22) = Application.Sum(Range(Cells(OperatingRow - CountOfMatching, 21), Cells(OperatingRow, 21)))
                  
                  If WB.Cells(OperatingRow - CountOfMatching, 22) = 0 Then
                      WB.Cells(OperatingRow - CountOfMatching, 23) = &quot;Text for Zero Total Payable&quot;
                  Else
                      WB.Cells(OperatingRow - CountOfMatching, 23) = &quot;Not Paid&quot;
                  End If
                  
                  WB.Cells(OperatingRow - CountOfMatching, 22).NumberFormat = &quot;$#,##0.00&quot;
  
              CountOfMatching = 0
              
          End If
          
      Next OperatingRow
      
      If WB.Cells(LastRow + 2, 21) = WB.Cells(LastRow + 2, 22) Then
          WB.Range(Cells(LastRow + 2, 21), Cells(LastRow + 2, 22)).BorderAround ColorIndex:=4, Weight:=xlMedium
      Else
          WB.Range(Cells(LastRow + 2, 21), Cells(LastRow + 2, 22)).BorderAround ColorIndex:=3, Weight:=xlMedium
      End If
  
  End Sub
  </code></pre>
  <p><a href="https://i.stack.imgur.com/XESP6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XESP6.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/x8A1i.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x8A1i.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/nzqYQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nzqYQ.png" alt="enter image description here" /></a></p>