VBA/Excel/Access/Word/Language Basics/Static variable
Содержание
If you need a procedural level variable to retain its value between calls to the procedure, declare the variable using the Static keyword
<source lang="vb">
Private Sub MyProcedure2()
Static myVar As Integer myVar = myVar + 1
End Sub
</source>
Redefine static variable
<source lang="vb">
Sub Scope1()
Static Sales Sales = Sales + 1 MsgBox Sales
End Sub Sub Scope2()
Static Sales Sales = Sales + 10 MsgBox Sales
End Sub
</source>
Static variable lifetime
<source lang="vb">
Sub LifeTime()
Static Sales Sales = Sales + 1 MsgBox Sales
End Sub
</source>
Static Variables: A Special Type of Local Variable
<source lang="vb">
Private Sub cmdLocalAge_Click()
Dim intAge As Integer intAge = intAge + 1 Debug.Print intAge
End Sub Private Sub cmdStaticAge_Click()
Static sintAge As Integer sintAge = sintAge + 1 Debug.Print sintAge
End Sub
</source>