none
Want locate a cell through string,then how... RRS feed

  • Question

  • I need to locate a single cell based on a known text string that will be in it. Please could you point me in the right direction if this is possible? In Excel 2013,and I use C#. Any help, thank you!
    Monday, April 27, 2015 8:12 AM

Answers

  • Hi jujubeee,

    According to the description, you want to find the specific text in the worksheet. As far as I know, we can use Range.Find to find the text we wanted and I also wrote a simple sample for your reference:

      Application excelApp = new Application();
                excelApp.Visible = true;
                string fileName = @"C:\book1.xlsx";
                Workbook wb=excelApp.Workbooks.Open(fileName);
                Range usedRange = wb.Worksheets["Sheet1"].UsedRange;
                wb.Worksheets["Sheet1"].Range["A1"].Activate();
                Range findedRange=usedRange.Find("Hello");
                string firstAddress;
                if (findedRange != null)
                {
                    firstAddress = findedRange.Address;
                    do
                    {
                        findedRange.Font.Color = XlRgbColor.rgbDarkRed;
                        findedRange = usedRange.FindNext(findedRange);
                    }
                    while (findedRange != null & findedRange.Address!=firstAddress);
                }
            }

    You can get more detail about this function from link below:
    Range.Find Method (Excel)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 28, 2015 3:37 AM
    Moderator

All replies

  • You can refer this post Find text in string with C#, (http://stackoverflow.com/questions/4734116/find-and-extract-numbers-from-a-string)it offered some useful feedback. sorry can't paste link now,seem my standard is too low.

    OR u can try some third party Excel library, just download hoxfix and add .dll files to your project, then use the following code.(http://www.e-iceblue.com/Introduce/excel-for-net-introduce.html)

    Workbook book = new Workbook();
    book.LoadFromFile("test.xlsx");
    foreach(CellRange cell in book.Worksheets[0].Cells)
                {
                    if(cell.Text=="test")
                    {
                        Console.WriteLine(cell.RangeAddress);
                    }
                }
    Console.ReadLine();

    Monday, April 27, 2015 8:24 AM
  • Hi jujubeee,

    According to the description, you want to find the specific text in the worksheet. As far as I know, we can use Range.Find to find the text we wanted and I also wrote a simple sample for your reference:

      Application excelApp = new Application();
                excelApp.Visible = true;
                string fileName = @"C:\book1.xlsx";
                Workbook wb=excelApp.Workbooks.Open(fileName);
                Range usedRange = wb.Worksheets["Sheet1"].UsedRange;
                wb.Worksheets["Sheet1"].Range["A1"].Activate();
                Range findedRange=usedRange.Find("Hello");
                string firstAddress;
                if (findedRange != null)
                {
                    firstAddress = findedRange.Address;
                    do
                    {
                        findedRange.Font.Color = XlRgbColor.rgbDarkRed;
                        findedRange = usedRange.FindNext(findedRange);
                    }
                    while (findedRange != null & findedRange.Address!=firstAddress);
                }
            }

    You can get more detail about this function from link below:
    Range.Find Method (Excel)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, April 28, 2015 3:37 AM
    Moderator