vba variable code example
Example 1: vba declare variable with value
Dim myString As String: myString = "aaa bbb"
Dim myStringCaps As String: myStringCaps = UCase("aaa bbb")
Dim myPart As Variant: myPart = Split(myString)
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(1)
Const MY_CONST As String = "Value"
Example 2: vba static variable
' Static variable inside a function
Function MyStaticFunction(ByVal pToAdd As Integer) As Integer
Static iStatic As Integer ' Keeps its value between calls
iStatic = iStatic + pToAdd
MyStaticFunction = iStatic
End Function
'---------------------------------------------------------------------
Sub TesMe()
Debug.Print MyStaticFunction(1) ' 1
Debug.Print MyStaticFunction(3) ' 4
End Sub