Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Document-Processing-toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -6364,6 +6364,9 @@
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-check-whether-an-excel-document-contains-macro">How to check whether an Excel document contains macro?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-check-if-a-formula-in-a-cell-contains-an-error-value">How to check if a formula in a cell contains an error value?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/does-xlsio-support-password-protected-macro-in-the-excel-documents">Does XlsIO support password protected macro in the Excel documents?</a>
</li>
Expand Down Expand Up @@ -6772,6 +6775,9 @@
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-merge-cells-preserving-topleft-value-and-format-in-xlsio">How to merge cells preserving topleft value and format in XlsIO?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-check-if-a-formula-in-a-cell-contains-an-error-value">How to check if a formula in a cell contains an error value?</a>
</li>
</ul>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
title: How to Check Formula Error Values in a Cell | Syncfusion
description: This page describes how to check if a formula returns an error and how to access the corresponding error value using XlsIO.
platform: document-processing
control: XlsIO
documentation: UG
---

# How to check if a formula in a cell contains an error value?

Formulas are calculated during execution. Once calculated, you can check if the result has an error and retrieve the corresponding error value, such as #DIV/0! or #N/A, using built-in error-checking support.

The example below shows how to iterate the used range of a worksheet, check each cell for `HasFormulaErrorValue`, and print the `FormulaErrorValue` and the cell address.

{% tabs %}
{% highlight c# tabtitle="C# [Cross-platform]" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Input/InputTemplate.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];

IRange usedRange = worksheet.UsedRange;
int firstrow = usedRange.Row;
int lastrow = usedRange.LastRow;
int firstcol = usedRange.Column;
int lastcol = usedRange.LastColumn;

for (int row = firstrow; row <= lastrow; row++)
{
for (int col = firstcol; col <= lastcol; col++)
{
if (worksheet[row, col] != null && worksheet[row, col].HasFormulaErrorValue)
{
Console.WriteLine($"Formula error value: {worksheet[row,col].FormulaErrorValue} in Address: {worksheet[row,col].AddressLocal}");
}
}
}

workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
}
{% endhighlight %}

{% highlight c# tabtitle="C# [Windows-specific]" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];

IRange usedRange = worksheet.UsedRange;
int firstrow = usedRange.Row;
int lastrow = usedRange.LastRow;
int firstcol = usedRange.Column;
int lastcol = usedRange.LastColumn;

for (int row = firstrow; row <= lastrow; row++)
{
for (int col = firstcol; col <= lastcol; col++)
{
if (worksheet[row, col] != null && worksheet[row, col].HasFormulaErrorValue)
{
Console.WriteLine($"Formula error value: {worksheet[row,col].FormulaErrorValue} in Address: {worksheet[row,col].AddressLocal}");
}
}
}

workbook.SaveAs("Output.xlsx");
}
{% endhighlight %}

{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)

Dim usedRange As IRange = worksheet.UsedRange
Dim firstrow As Integer = usedRange.Row
Dim lastrow As Integer = usedRange.LastRow
Dim firstcol As Integer = usedRange.Column
Dim lastcol As Integer = usedRange.LastColumn

For row As Integer = firstrow To lastrow
For col As Integer = firstcol To lastcol
If worksheet(row, col) IsNot Nothing AndAlso worksheet(row, col).HasFormulaErrorValue Then
Console.WriteLine("Formula error value: " & worksheet(row, col).FormulaErrorValue & " in Address: " & worksheet(row, col).AddressLocal)
End If
Next
Next

workbook.SaveAs("Output.xlsx")
End Using
{% endhighlight %}
{% endtabs %}

A complete working example in C# is present on <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/1014444-FormulaError/FAQ/FormulaError%20Value/.NET/FormulaError%20Value">this GitHub page</a>.