title: Variables declared with the Static statement
body: <p>I have this function written in VBA that I would like to convert to Apps Script, but I have not found anything similar to declare a variable of static type to keep the range object saved in the rngLast variable with multiple executions of function onSelectionChange (e):</p>
  <pre><code>Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Static rngLast As Range
  If Not Application.Intersect(Target, Range(&quot;A1:Z100&quot;)) Is Nothing Then
      If Not rngLast Is Nothing Then
      If rngLast.Address &lt;&gt; Target.Address Then
      MsgBox &quot;Lost focus &quot; &amp; rngLast.Address
      End If
      End If
      
      MsgBox &quot;Got focus &quot; &amp; Target.Address
      Set rngLast = Target
  End If
  End Sub
  </code></pre>
  <p>In Apps Script I wrote it like this:</p>
  <pre><code>var rngLast;
  
  function onSelectionChange(e) {
  var spreadsheet = SpreadsheetApp.getActive();
  var sh = spreadsheet.getActiveSheet();
  var rowActCell = e.range.getRow();
  var colActCell = e.range.getColumn();
  
    if((rowActCell &gt;=1 &amp;&amp; rowActCell &lt;=100) &amp;&amp; (colActCell &gt;=1 &amp;&amp; colActCell &lt;= 26)) {
      if(rngLast != undefined || rngLast != null) {
        if(rngLast.getA1Notation() != e.range.getA1Notation()) {
          Browser.msgBox(&quot;Lost focus &quot; + rngLast.getA1Notation());
        }
      }
      Browser.msgBox(&quot;Got focus &quot; + e.range.getA1Notation());
      rngLast = e.range
    }
  }
  </code></pre>
  <p>but I can't keep the value of e.range saved in rngLast so it doesn't work. In practice, every time onSelectionChange (e) is activated by changing the selection cell,  rngLast assumes the undefined state.
  To test how it works I replaced:
  var rngLast;
  with
  var rngLast = SpreadsheetApp.getActive().getActiveSheet().getRange(&quot;A1&quot;);</p>
  <p>I hope with my bad English that I have explained myself ;-)</p>
  <p>Thanks bye
  Gabriele</p>