Static Statement

Description

Used at the procedure level to declare variables and allocate storage space. Variables declared with the Static statement retain their value as long as the code is running.

Syntax

Static varname[([subscripts])][As type][,varname[([subscripts])][As type]] . . .

The Static statement syntax has these parts:

Part

Description

varname

Name of the variable; follows standard variable naming conventions.

subscripts

Dimensions of an array variable; up to 60 multiple dimensions may be declared. The subscripts argument uses the following syntax:

[lower To] upper [,[lower To] upper] . . .

type

Data type of the variable; may be Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable being defined.


Remarks

Once the module code is running, variables declared with the Static statement retain their value until the module is reset or restarted. Use the Static statement in nonstatic procedures to explicitly declare Static variables.

Use a Static statement within a procedure to declare the data type of a Static variable. For example, the following statement declares a fixed-size array of integers:


Static EmployeeNumber(200) As Integer

If you do not specify a data type or object type, and there is no Deftype statement in the module, the variable is Variant by default.

Note

The Static statement and the Static keyword affect the lifetime of variables differently. If you declare a procedure using the Static keyword (as in Static Sub CountSales ()), the storage space for all local variables within the procedure is allocated once and the value of the variables is preserved for the entire time the code is running. For nonstatic procedures, storage space for variables is allocated each time the procedure is called and released when the procedure is exited. The Static statement is used to declare variables within nonstatic procedures to preserve their value as long as the program is running.

When variables are initialized, a numeric variable is initialized to 0, a variable-length string is initialized to a zero-length string, and a fixed-length string is filled with zeros. Variant variables are initialized to Empty. Each element of a user-defined type variable is initialized as if it was a separate variable. A variable that refers to an object must be assigned an existing object using the Set statement before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it does not refer to any particular instance of an object.

Tip

When you use the Static statement in a procedure, it is a generally accepted programming practice to put the Static statement at the beginning of the procedure with any Dim statements.

See Also

Array Function, Dim Statement, Function Statement, Option Base Statement, Private Statement, Public Statement, ReDim Statement, Sub Statement.

Example

This example uses the Static statement to retain the value of a variable as long as module code is running.


' Function definition.
Function KeepTotal(Number)
    ' Only the variable Accumulate preserves its value between calls.
    Static Accumulate
    Accumulate = Accumulate + Number
    KeepTotal = Accumulate
End Function

' Static function definition.
Static Function MyFunction(Arg1, Arg2, Arg3)
    ' All local variables preserve value between function calls.
    Accumulate = Arg1 + Arg2 + Arg3
    Half = Accumulate / 2
    MyFunction = Half
End Function

This example uses the worksheet function Pmt to calculate a home mortgage loan payment. Note that this example uses the InputBox method instead of the InputBox function so that the method can perform type checking. The Static statements cause Visual Basic to retain the values of the three variables; these are displayed as default values the next time you run the example.


Static loanAmt
Static loanInt
Static loanTerm
loanAmt = Application.InputBox _
    (Prompt:="Loan amount (100,000 for example)", _
        Default:=loanAmt, Type:=1)
loanInt = Application.InputBox _
    (Prompt:="Annual interest rate (8.75 for example)", _
        Default:=loanInt, Type:=1)
loanTerm = Application.InputBox _
    (Prompt:="Term in years (30 for example)", _
        Default:=loanTerm, Type:=1)
payment = Application.Pmt(loanInt / 1200, loanTerm * 12, loanAmt)
MsgBox "Monthly payment is " & Format(payment, "Currency")