Used inside a procedure scope, the Dim keyword declares a local variable.
Inside a procedure scope, you can only declare local variables; local variables live & die within the scope they're declared in.
If you need to access a variable from more than one single scope, you have several options:
- Promote the local variable to global scope by moving its declaration into a module's declarations section, changing the keyword to
Public: the variable is now accessible from all procedure scopes in all modules. That's usually a rather bad idea.
- Promote the local variable to module level (move its declaration into the module's declarations section), either keeping the
Dim keyword or changing it to Private: the variable is now accessible from all procedure scopes in that module. Likely also a bad idea.
Note that Public and Private keywords (and the deprecated Global keyword too) are illegal in local scope.
There are other options, but before we look into them let's re-look at this statement:
In "Public Sub PackageDimensionsCalculator()" which is a result of some calculations.
A Sub procedure does not have a "result". Sub procedures do something, they don't return any results. We can make it actually return a result two ways:
ByRef return: we could declare a ByRef parameter, and use it to "return" the result:
Public Sub PackageDimensionsCalculator(ByRef outResult As Single)
'...do the calculations...
outResult = ... ' <~ assign the return value before procedure exits!
End Sub
Change it to a Function. Function procedures have one purpose: take inputs, compute a result, and return it:
Public Function PackageDimensionsCalculator() As Single
'...do the calculations...
PackageDimensionsCalculator = ... ' <~ assign the return value before procedure exits!
End Sub
Now EvaluateFormatSketch() can get the value by invoking the function:
Public Function EvaluateFormatSketch() As Boolean
Dim pckgDimensions As Single
pckgDimensions = PackageDimensionsCalculator
'return a Boolean value by assigning to the function identifier:
EvaluateFormatSketch = pckgDimensions > 0
End Function
Or with a ByRef return:
Public Function EvaluateFormatSketch() As Boolean
Dim pckgDimensions As Single
PackageDimensionsCalculator outResult:=pckgDimensions
'return a Boolean value by assigning to the function identifier:
EvaluateFormatSketch = pckgDimensions > 0
End Function
Consider renaming PackageDimensionsCalculator so that its name begins with a verb, e.g. CalculatePackageDimensions, noting that it's suspicious to have a procedure that calculates something but doesn't take any inputs. Consider refactoring any inputs as parameters, making the caller responsible for getting the values that affect the calculation - the function will be much easier to test/validate (and eventually, to maintain/debug) if all its inputs & dependencies are passed as parameters.
Subinto aFunctionand return the value.