title: Static 'function' in vba/excel?
body: <p>I just have a very simple question regarding vba.</p>
  
  <p>I understand that a <strong>static variable</strong> in vba keeps its previous value rather then reseting it each time the function is called. However, I was wondering how could I have a "static function" rather than a variable. Let me clarify this with a simple code:</p>
  
  <pre><code>Function Alt(Soc1 As Double)
  Static D As Integer
  
  If Soc1 &gt;= 40 Then
      Alt = 0
  ElseIf Soc1 &lt;= 30 Then
      Alt = 1
  End If
  
  End Function
  </code></pre>
  
  <p>Here I would like the variable-function <strong>Alt</strong> to keep its previous value so that I get a sort of "hysteresis" behaviour. However if I add:</p>
  
  <pre><code>Static Alt As Integer
  </code></pre>
  
  <p>I get an error when calling the function Alt().</p>
  
  <p>Also if I add a line such as:<code>Alt = D</code> while D is decalred as static I get an alternating value of Alt (1,0,1,0,1,0 ..) instead of (1,1,1,1) if active or (0,0,0,0) if not.</p>
  
  <p>How should I tackle this simple issue? Any help would be appreciated.</p>
  
  <p>Thank you in advance!</p>