title: Cleaning cells with a VBA function
body: <p>I want to create a VBA function, <code>myFunction(),</code> that writes content in cells and when the calculation is done, it would clear all the data it wrote. (I want to call it from a cell with <code>=myFunction()</code>) To clear the content, I've put this line at the end, to clean up before finishing:</p>
  
  <pre><code>ActiveSheet.Range("$A$1:$B$9").ClearContents
  </code></pre>
  
  <p>The problem is that is that <strong>it doesn't clear anything</strong>. However, I noticed that if I put that line above in a subroutine, and then assign that subroutine to a button, the content will be cleared when I click the button.</p>
  
  <p>When I run the code from the window of the program "Microsoft Visual Basic" (with the play button), the code works perfectly (the content gets cleared) but when I call the function from a cell, the cleaning part doesn't work anymore. 
  Here is the code:  </p>
  
  <pre><code>Function myFunction()
    ActiveSheet.Range("$A$1:$B$9").Clear
  End Function
  </code></pre>
  
  <p>When I click in a cell and type <code>=myFunction()</code>, the content in the range $A$1:$B$9 is NOT cleared. 
  However if I create a subroutine (instead of a function), and call it with a button, the content IS cleared.  </p>
  
  <p>Why won't it work when called by myFunction() ? How can I solve this ?</p>