Understanding and Using Static Variables : Variable Scope « Language Basics « VBA / Excel / Access / Word






Understanding and Using Static Variables

 
Sub CostOfPurchase()
    ' declare variables
    Static allPurchase
    Dim newPurchase As String
    Dim purchCost As Single

    newPurchase = InputBox("Enter the cost of a purchase:")
    purchCost = CSng(newPurchase)
    allPurchase = allPurchase + purchCost

    ' display results
    MsgBox "The cost of a new purchase is: " & newPurchase
    MsgBox "The running cost is: " & allPurchase
End Sub

 








Related examples in the same category

1.Local variable
2.Procedure-Level (Local) Variables
3.module-level variable declared with Private statement
4.Understanding Module-Level Variables
5.Public Variables
6.sub module scope
7.Module level variable
8.Local variables are available only in the procedure where they are declared
9.Private module variable