title: Application-defined or object-defined error with formula
body: <p>I get an "application-defined or object-defined error" with the last formula block in this code. The other two work fine, and the last formula, when inserted into a cell, works perfectly fine there, too.</p>
  
  <p>Any ideas what throws the error?</p>
  
  <pre><code>Sub Wells_cartesian_to_spherical()
  Range("M1").Value = "Boret lengde"
  Range("NJ1").Value = "Asimuth"
  Range("O1").Value = "Boret helning"
  
  Dim RngX2 As Range
  Set RngX2 = Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp)).Offset(, 11)
  RngX2.Formula = "=SQRT(((B2-I2)^2+(C2-J2)^2)+(D2-K2)^2)"
  RngX2.Value = RngX2.Value
  
  Dim RngY2 As Range
  Set RngY2 = Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp)).Offset(, 13)
  RngY2.Formula = "=DEGREES(ASIN(SQRT((B2-I2)^2+(C2-J2)^2)/(SQRT(((B2-I2)^2+(C2-J2)^2)+(D2-K2)^2))))"
  'RngY2.Value = RngY2.Value
  
  Dim RngZ2 As Range
  Set RngZ2 = Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp)).Offset(, 12)
  RngZ2.Formula = "=DEGREES(IF(I2-B2&gt;0,(PI()/2)-((ATAN((J2-C2)/(I2-B2)))),IF(I2-B2&lt;0,((3*PI())/2)-((ATAN((J2-C2)/(I2-B2)))),IF(J2-C2&lt;0,PI(),0))))"
  RngZ2.Value = RngZ2.Value
  End Sub
  </code></pre>
  
  <p>The formula is used to calculate well depth, well orientation and well inclination for a drilled well given the x, y and z coordinates for the top and bottom of the well. Below is some comma separated sample data which should work with code:</p>
  
  <pre><code>Well no,X,Y,Z,Drilled length,Azimuth,Inclination,objtype,X bunn,Y bunn,Z bunn
  28738,83124.42153,1233423.379,121,120,0,0,Energibrønn,83124.42153,1233423.379,1
  29674,87717.14427,1237872.851,139.8000031,135,0,0,Energibrønn,87717.14427,1237872.851,4.800003052
  29652,90488.64255,1242007.401,229.8000031,201,0,0,Energibrønn,90488.64255,1242007.401,28.80000305
  28791,90899.79513,1243677.054,274.1000061,114,0,0,Energibrønn,90899.79513,1243677.054,160.1000061
  29171,88224.0884,1233361.655,127.9000015,102,100,10,Energibrønn,88241.53142,1233358.579,27.44961072
  30393,88204.31375,1233372.11,127.5999985,160,125,2,Energibrønn,88208.88783,1233368.907,-32.30253385
  </code></pre>
  
  <p>Just copy and paste to Excel. Please excuse any Norwegian characters/words ;)</p>