Formulas within existing rows are corrupted when a new row is added. Formulas that reference another worksheet within the same workbook are corrupted when a new row is inserted. The error is occurring in method 'ExcelCellBase.UpdateFormulaReferences'. This method is attempting to adjust addresses that might have shifted as a result of the newly inserted row. The method currently fails to check whether the addresses within the formula refer to another worksheet. As a result, the method attempts to adjust the formula address when it should not do so, and thereby corrupts the formula.
We have circumvented the problem by updating the 'UpdateFormulaReferences' method to check whether the address within the formula refers to another worksheet before adjusting the address. In file ExcelCellBase.cs at line 859, we added the 'if (!String.IsNullOrEmpty' statement below.
```
foreach (var t in tokens)
{
if (t.TokenType == TokenType.ExcelAddress)
{
var a = new ExcelAddressBase(t.Value);
if (!String.IsNullOrEmpty(a._ws))
{
// This address is in a different worksheet, thus no update is required
f += a.Address;
continue;
}
__```
The attached example sheet illustrates the problem. The first worksheet contains an integer value in the first column and a 'VLOOKUP' formula in the second column that refers to a lookup table on the second worksheet. The integer value in the first column is the argument to the VLOOKUP formula. The first worksheet contains three data rows. You can reproduce the problem by inserting a new row near the top of the worksheet. You will observe that the formulas in all of the existing rows are corrupted following the insert.
We have circumvented the problem by updating the 'UpdateFormulaReferences' method to check whether the address within the formula refers to another worksheet before adjusting the address. In file ExcelCellBase.cs at line 859, we added the 'if (!String.IsNullOrEmpty' statement below.
```
foreach (var t in tokens)
{
if (t.TokenType == TokenType.ExcelAddress)
{
var a = new ExcelAddressBase(t.Value);
if (!String.IsNullOrEmpty(a._ws))
{
// This address is in a different worksheet, thus no update is required
f += a.Address;
continue;
}
__```
The attached example sheet illustrates the problem. The first worksheet contains an integer value in the first column and a 'VLOOKUP' formula in the second column that refers to a lookup table on the second worksheet. The integer value in the first column is the argument to the VLOOKUP formula. The first worksheet contains three data rows. You can reproduce the problem by inserting a new row near the top of the worksheet. You will observe that the formulas in all of the existing rows are corrupted following the insert.