Home » excel » c# 4.0 – Excel: Find data in hidden cells (without unhiding them)

c# 4.0 – Excel: Find data in hidden cells (without unhiding them)

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a find function that looks like this:

public static int FindRowThatContains(Excel.Worksheet ws, string what)
{
    int result = 0;
    Excel.Range rng = ws.Cells.Find(what,
                    Type.Missing,
                                    Excel.XlFindLookIn.xlValues,
                                    Excel.XlLookAt.xlWhole,
                                    Type.Missing,
                                    Excel.XlSearchDirection.xlNext,
                                    false,
                                    Type.Missing,
                                    Type.Missing);

    if (rng != null)
        result = rng.Row;

    return result;
}

This works when the cell containing the data I’m looking for isn’t hidded but it fails when the cell searched for is missing.

Is there any way to include hidden cells in the search?

Thx

How to&Answers:

Try XlFindLookIn.xlFormulas instead of XlFindLookIn.xlValues.

The Range.Find method appears to replicate the find dialog in Excel. With formulas selected instead of values, the find dialog will find a hidden cell.

Of course, that might cause other problems if you don’t want to search in formulas as well, but using this method, it looks like the only way.