title: How to re-calculate a cell's formula?
body: <p>My code is setting up a lot cell values. At the end, formulas in every cell needs to be evaluate before the excel file is generated. For most of the sheets, things are going well.</p>
  
  <p>However, there is a cell that is throwing an exception. That cell is calculating the average of a series of cells that come after it and reference cells in other sheets. I guess that when the first cell is trying to calculate the average, the cells after are not evaluated yet.</p>
  
  <p>This is the formula on that cell</p>
  
  <pre><code>=IFERROR(AVERAGEIF(D2:AU2,"&lt;&gt;-"),"-")
  </code></pre>
  
  <p>I suspect that it's because cells int that range reference cells in other sheet. <code>D2</code>, for instance, has the following formula <code>=Common!E2</code>.</p>
  
  <p>So what I decided to do is to:</p>
  
  <ol>
  <li>Wrap the call to execute a formula in a <code>try-catch</code> block.</li>
  <li>If executing a cell throws an exception, I save coordinates of that cell.</li>
  <li>Then I execute the cells that threw exceptions </li>
  </ol>
  
  <p>I've noticed that the same cell keeps throwing error. Just to be able to exit the loop, I'm counting to 3 iterations. </p>
  
  <p>When the excel generate is generated, that cells has the value <code>-</code>. However, when I select the cell then press <code>enter</code>, it executes and displays the right value.</p>
  
  <p>The reason I'm postponing the execution of that cell is that, after the first iteration, cells referencing other sheets are already evaluated. But, it still not working as it keeps throwing exception.</p>
  
  <p>Thanks for helping.</p>
  
  <h1>Update</h1>
  
  <p>I'm using npoi and c#.</p>