title: Excel VBA. When is it necesary to activate a Sheet?
body: <p>A general answer would be perfect, b
  I have this simple code, that only works if Sheet2 is active when running the macro.</p>
  
  <p>Otherwise it gives an error "defined by the application or object":</p>
  
  <pre><code>Sub PasteArray()
  Dim arr(1 To 3) As Variant
  Dim n As Integer
  Dim ws As Worksheet
  
  arr(1) = 4
  arr(2) = 6
  arr(3) = 8
  n = UBound(arr) - LBound(arr) + 1
  Sheets("Sheet2").Range(Cells(1, 1), Cells(n, 1)) = WorksheetFunction.Transpose(arr)
  End Sub
  </code></pre>
  
  <p>It works if I activate the sheet first, but that requires that I save the current active sheet to come back at the end.</p>
  
  <pre><code>Sub PasteArray()
  Dim arr(1 To 3) As Variant
  Dim n As Integer
  Dim ws As Worksheet
  
  arr(1) = 4
  arr(2) = 6
  arr(3) = 8
  n = UBound(arr) - LBound(arr) + 1
  Set ws = ActiveSheet
  Worksheets("sheet2").Activate
  Range(Cells(1, 1), Cells(n, 1)) = WorksheetFunction.Transpose(arr)
  ws.Activate
  </code></pre>
  
  <p>Is it always necesary to do something like this?
  I am pretty sure that I have seen some code in which changes to cells in an unactive sheet were made.</p>
  
  <p>Thank you</p>