I understand that is some of VB basics, but can't figure out how to ask google:
How VBA manage constants? In some languages compiler replace them by values. But how that works in VBA? Will it matter if i declare them in sub/function, not in global space. Especially, if a sub/function is called many times in runtime.
I'm often declare function name and some other strings as constants in sub/function space - it's easier for me to read my code. For example: SUB_NAME in Get_AppExcel_Ref() in code below, to use it for logging error events. If the Get_AppExcel_Ref() will be called couple times while programm running - SUB_NAME will be allocated in memory once, on first run, or on every call? Or maybe there is some kind perfomance issue and better declare SUB_NAME as global.
Private Sub Get_AppExcel_Ref(ByRef appObject As Object)
Const SUB_NAME As String = "Get_AppExcel_Ref"
On Error GoTo ERR_NOT_OPENNED
Set appObject = GetObject(, "Excel.Application")
Exit Sub
ERR_NOT_OPENNED:
If Err.Number = 429 Then
Err.Clear
Set appObject = CreateObject("Excel.Application")
Else
Call LOG.printLog("open Excel", Err, SUB_NAME)
End If
End Sub
'LOG - user class type variable, printLog params: Description, Error, Source Name
Constdeclared local has local scope so it will not be known outside that scope.Constsuch as your string, will be stored somewhere in memory (otherwise its value will not be known). So for memory usage it doesn't matter if you declare it local or global.Constin VB is probably implemented as a read-only variable. It will exist only once in memory, independent of how often you call the fuction where it is declared. The compiler/interpreter may fold multiple identical Const values onto a single value, thus saving memory. But I wouldn't worry about these things for VB.Private Subfollowed byEnd Function