Clear Cell range values and preserve formulas

Hello,

I am using Cells.ClearContents(Int32, Int32, Int32, Int32) to clean up a range of cells.
The method works fine, however, I noticed that it deletes the formulas as well.
Is there a method that clears a cell range while also preserving the formulas?

Alternatively, is there a method that checks if a given cell contains a formula?
I’m thinking of changing the way I clear the content. I could iterate over each cell and clear it if it doesn’t contain a formula.

@stefbats,
You may use Cell.IsFormula property to check if cell contains a formula or not. Give a try to the following sample code and share the feedback.

Workbook wb = new Workbook("book1.xlsx");
for(int i = 0; i < 20; i++)
{
    Cell cell = wb.Worksheets[0].Cells[i, 1];
    if (!cell.IsFormula)
    {
        cell.Value = "";
    }
}
wb.Save("book2.xlsx");

Book1.zip (6.4 KB)
book2.zip (7.0 KB)

Hello.

I tried your code, it’s working.

Thank you for the help :slightly_smiling_face:

@stefbats,

Good to know that the suggested code segment is helpful to you. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.