ValueOfFormula

Converts VALUE_OF custom formulas written with dictionary variable names into workbook-ready expressions that supply sheet names and column indices. The class validates that all three arguments (key variable, lookup variable, value variable) exist in the dictionary and share the same worksheet, then resolves column indices for downstream consumption. Consumers interact through the IValueOfFormula interface.

Depends on: BetterArray, LLdictionary, LLVariables, LLSheets

Version: 1.0 (2026-02-09)

Factory

create #

Instantiate a VALUE_OF parser bound to the supplied dictionary

Signature:

Public Function Create(ByVal formula As String, _
                       ByVal dict As ILLdictionary, _
                       Optional ByVal variables As ILLVariables, _
                       Optional ByVal sheets As ILLSheets) As IValueOfFormula

Validates that the dictionary is not Nothing, stores all dependencies, and returns the IValueOfFormula interface. Parsing is deferred until the first access to Valid, ConvertedFormula, or any result property. Optional variables and sheets helpers can be supplied to avoid redundant instantiation when the caller already holds them.

Parameters:

  • formula: String. Representation of the VALUE_OF call.
  • dict: ILLdictionary. The dictionary exposing variables and sheets metadata.
  • variables: Optional ILLVariables. Reuse an existing helper instance.
  • sheets: Optional ILLSheets. Reuse an existing helper instance.

Returns: IValueOfFormula. A ready-to-inspect parser instance.

Throws:

  • ProjectError.ObjectNotInitialized When dict is Nothing.

Validation

valid #

Determine whether the VALUE_OF expression parsed successfully

Signature:

Public Property Get Valid() As Boolean

Parsed results and diagnostic properties. Triggers lazy parsing on first access and returns the cached validity flag. True indicates that all three arguments were resolved against the dictionary and the lookup/value variables share the same sheet.

Returns: Boolean. True when parsing and dictionary lookups succeeded.


converted-formula #

Retrieve the workbook-ready VALUE_OF expression

Signature:

Public Property Get ConvertedFormula() As String

Returns the converted expression with the sheet name and column indices resolved. Returns vbNullString when the formula is invalid.

Returns: String. Converted expression; vbNullString when invalid.


failure-reason #

Provide a descriptive message when parsing fails

Signature:

Public Property Get FailureReason() As String

Returns a human-readable explanation of the validation failure. Returns vbNullString when the formula is valid.

Returns: String. Description; vbNullString for valid formulas.


lookup-sheet-name #

Expose the resolved lookup worksheet name

Signature:

Public Property Get LookupSheetName() As String

Returns the worksheet name where the lookup and value variables reside. Available only after successful parsing.

Returns: String. Sheet name or vbNullString when unavailable.


lookup-column-index #

Expose the column index used to locate the matching value

Signature:

Public Property Get LookupColumnIndex() As Long

Returns the 1-based column index of the lookup variable within the dictionary. Zero when the formula is invalid.

Returns: Long. Lookup column index; zero when the formula is invalid.


value-column-index #

Expose the column index providing the returned value

Signature:

Public Property Get ValueColumnIndex() As Long

Returns the 1-based column index of the value variable within the dictionary. Zero when the formula is invalid.

Returns: Long. Value column index; zero when the formula is invalid.


Internal members (not exported)

PublicAccessors

self #

Current object instance

Signature:

Public Property Get Self() As IValueOfFormula

Properties that expose internal state and the interface pointer. Returns the current instance cast to its interface. Used internally by the factory method to return an IValueOfFormula reference.

Returns: IValueOfFormula. Reference to the current object.


dictionary #

Retrieve the dictionary backing this instance

Signature:

Public Property Get Dictionary() As ILLdictionary

Returns the ILLdictionary reference that was supplied at creation and is used during lazy parsing.

Returns: ILLdictionary. The dictionary used during parsing.


dictionary-set #

Assign the dictionary and reset cached state

Signature:

Public Property Set Dictionary(ByVal dict As ILLdictionary)

Replaces the dictionary reference, clears the lazily created variable and sheet helpers, and resets all parsed outcomes so the next access triggers a fresh evaluation.

Parameters:

Throws:


value-of-expression #

Retrieve the stored VALUE_OF expression

Signature:

Public Property Get ValueOfExpression() As String

Returns the formula text as stored after trimming during assignment.

Returns: String. Expression as provided to the parser.


value-of-expression-set #

Store the VALUE_OF expression and reset cached results

Signature:

Public Property Let ValueOfExpression(ByVal formula As String)

Trims the incoming formula and stores it, then clears all cached parsing outcomes so the next access triggers a fresh parse.

Parameters:


Parsing

ensure-parsed #

Lazily parse the VALUE_OF expression and cache outcomes

Signature:

Private Sub EnsureParsed()

Lazy parsing engine and argument extraction. Executes the full parsing pipeline on first call: extracts three arguments, validates each against the dictionary, confirms sheet alignment, resolves column indices, and builds the converted formula. All outcomes are cached so subsequent calls are no-ops.


extract-arguments #

Split the VALUE_OF body into trimmed argument segments

Signature:

Private Function ExtractArguments() As BetterArray

Walks the sanitised body character by character, tracking quotation state and parenthesis depth. Splits on top-level commas and returns exactly three arguments for a well-formed VALUE_OF expression.

Returns: BetterArray. Up to three arguments; Nothing when malformed.


sanitised-formula #

Remove the VALUE_OF token and surrounding parenthesis

Signature:

Private Function SanitisedFormula() As String

Strips the VALUE_OF token, opening parenthesis, and closing parenthesis to expose the raw comma-delimited argument body.

Returns: String. The argument list; vbNullString when malformed.


build-converted-formula #

Compose the converted VALUE_OF expression

Signature:

Private Function BuildConvertedFormula(ByVal keyVar As String, _
                                       ByVal sheetName As String, _
                                       ByVal lookupIndex As Long, _
                                       ByVal valueIndex As Long) As String

Assembles the VALUE_OF output string with the key variable name, the quoted sheet name, and the two resolved column indices.

Parameters:

Returns: String. Workbook-ready VALUE_OF expression.


quoted-text #

Wrap a value in double quotes while escaping embedded quotes

Signature:

Private Function QuotedText(ByVal value As String) As String

Parameters:

Returns: String. Quoted representation suitable for Excel formulas.


Helpers

initialise #

Assign dependencies and prime the parser state

Signature:

Public Sub Initialise(ByVal formula As String, _
                       ByVal dict As ILLdictionary, _
                       ByVal variables As ILLVariables, _
                       ByVal sheets As ILLSheets)

Initialisation, state management, and lazy dependency providers. Stores the dictionary, optional helpers, and formula, then resets all cached state so the parser is ready for lazy evaluation.

Parameters:


reset-state #

Clear cached parsing outcomes

Signature:

Private Sub ResetState()

Marks the instance as unparsed and clears all result fields so the next property access triggers a full reparse.


reset-parsed-outcome #

Reset fields populated during parsing

Signature:

Private Sub ResetParsedOutcome()

Clears validity, converted formula, failure reason, sheet name, and column indices to their default (empty/zero) values.


fail #

Record a parsing failure

Signature:

Private Sub Fail(ByVal message As String)

Stores the failure message and resets all result fields to their invalid defaults. Called from EnsureParsed on every error path.

Parameters:


variables-provider #

Lazily create the LLVariables helper

Signature:

Private Function VariablesProvider() As ILLVariables

Ensures the dictionary is ready, creates the LLVariables helper on first access, and caches it for subsequent calls.

Returns: ILLVariables. Helper for the current dictionary.

Throws:


sheets-provider #

Lazily create the LLSheets helper

Signature:

Private Function SheetsProvider() As ILLSheets

Ensures the dictionary is ready, creates the LLSheets helper on first access, and caches it for subsequent calls.

Returns: ILLSheets. Helper for the current dictionary.

Throws:


ensure-dictionary-ready #

Guard against missing dictionary references

Signature:

Private Sub EnsureDictionaryReady()

Raises a ProjectError when the dictionary is Nothing, preventing downstream NullReference issues in the variable and sheet providers.

Throws:


throw-error #

Raise a project-specific error

Signature:

Private Sub ThrowError(ByVal errNumber As ProjectError, ByVal message As String)

Wrapper around Err.Raise that standardises the source to the class constant CLASSNAME for consistent stack traces.

Parameters:

Throws:


Interface

IValueOfFormula_ConvertedFormula #

Signature:

Private Property Get IValueOfFormula_ConvertedFormula() As String

Delegated members satisfying the IValueOfFormula contract.


Used in (4 file(s))