title: Static Variables in VBA
body: <p>I have an excel workbook where the user imports text files into a "Data Importation Sheet". The number of files imported is dependent on how files the user wants to import. So far my workbook works great but I have hit one bump. When the user imports a file an identifier (i.e. 1, 2, 3, etc) gets assigned to that data set. Then the user selects an option from a dropdown box and calculations and plots will automatically be produced. The user also has the option to "Clear all data" where when this is selected all worksheets are cleared and if the user imports new files (after clicking the "clear all data" button) the identifier value restarts at 1. Here is my code for the identifier/counting how many files have been imported..</p>
  
  <pre><code>Public Sub Macro(Optional reset As Boolean = False)
  
  Static i As Integer
  If reset Then
  i = -1
  i = i + 1
  Exit Sub
  End If
  
  i = i + 1
  
  Worksheets("Hidden").Cells(i + 1, 1).FormulaR1C1 = "=" &amp; i
  
  Worksheets("Hidden").Cells(2, 2).FormulaR1C1 = "=" &amp; i
  
  End Sub
  </code></pre>
  
  <p>The problem I have ran into now is data will need to be imported into this sheet at a later date so when I save this file and reopen it then import more files the identifier/count for file imports restarts at 1 which I do not want to happen. I want to be able to just keep adding more files and have the code continue, I do not want to have to clear all the imported data and restart. Any ideas as to how I can do this? TIA.</p>