title: Excel VBA formula, called from a cell, stops processing VBA, or encounters an App-Defined error, when a worksheet cell is written to
body: <p>I have this formula in a cell:</p>
  
  <pre><code>=GetData("Channel_01","Chicago")
  </code></pre>
  
  <p>Which executes this code:</p>
  
  <pre><code>Public Function GetData(ChannelCode As String, Key As String) As String
      Dim sql As String
      Dim cmd As New ADODB.Command
      Dim outputTo As Range
      Set outputTo = Application.Caller    
      sql = "select * from ChannelData WHERE ChannelCode = ? AND Key1 = ?"
      Set cmd = getCommand(sql, ChannelCode, Key)
      Dim rs As ADODB.Recordset
      Set rs = cmd.Execute
      WritePivotRecordset ChannelCode, rs, outputTo.Offset(1, 0)
  End Function
  
  Public Sub WritePivotRecordset(ChannelCode As String, rs As ADODB.Recordset, destination As Range)
      Dim i As Integer
      '*** WHEN THIS LINE OF CODE IS REACHED AND EXECUTES, PROCESSING STOPS
      Set destination.Value = ChannelCode
      For i = 1 To rs.Fields.Count - 1    'skip first field
          destination.Offset(0, i).Value = rs.Fields(i).Name
      Next
      destination.Offset(1, 0).CopyFromRecordset rs
  End Sub
  </code></pre>
  
  <p>The problem occurs on this line:</p>
  
  <pre><code>'*** WHEN THIS LINE OF CODE IS REACHED AND EXECUTES, PROCESSING STOPS
      Set destination.Value = ChannelCode
  </code></pre>
  
  <p>Is setting this invoking a spreadsheet recalc, which terminates the executing VBA thread or something like that?  I thought so, so I tried this before writing any output:</p>
  
  <pre><code>Application.Calculation = xlCalculationManual
  </code></pre>
  
  <p>But now on that same line of code I get:
  <em>Application-defined or object-defined error.</em></p>
  
  <p>Is writing from a VBA function to the same worksheet from which the VBA function is called, just not allowed?</p>