diff --git a/src/officecli/Core/Formula/FormulaEvaluator.cs b/src/officecli/Core/Formula/FormulaEvaluator.cs index d9f03efec..1564439a2 100644 --- a/src/officecli/Core/Formula/FormulaEvaluator.cs +++ b/src/officecli/Core/Formula/FormulaEvaluator.cs @@ -1194,6 +1194,10 @@ private void RestoreBindings(Dictionary snapshot) // ==================== Cell & Range Resolution ==================== + // External-workbook reference marker in a stored formula (the "[1]" in + // "[1]Sheet!A1"); such refs can't be resolved without the linked file. + private static readonly Regex ExternalRefRe = new(@"\[\d+\]", RegexOptions.Compiled); + internal FormulaResult? ResolveCellResult(string cellRef) { cellRef = StripDollar(cellRef).ToUpperInvariant(); @@ -1250,7 +1254,12 @@ private void RestoreBindings(Dictionary snapshot) { var circularBefore = _session.CircularHits; var evaluated = EvaluateFormula(ModernFunctionQualifier.Unqualify(cell.CellFormula.Text)); - if (evaluated != null) + // An external-workbook reference (=[1]Sheet!A1) can't be resolved without + // the linked file and surfaces as #REF!. Excel keeps the last cached + // for such cells, so fall through to it rather than poisoning every + // dependent total with #REF!. Genuine errors (no external link) still propagate. + if (evaluated != null + && !(evaluated.IsError && ExternalRefRe.IsMatch(cell.CellFormula.Text))) { // Memoize only clean results: no live bindings (see lookup // guard above) and no circular fallback during this