none
How to find row number in worksheet based on search string in c#.net RRS feed

  • Question

  • I am searching some string in the excel worksheet column. This is working perfect.

    But, i want to get the row number of the search string in the worksheet...?

    string colToCheck = "C40:C90"
    string SearchNo = "ABC-201";
    Excel.Range resultRange;
    colRange = XLWorkSheet1.Range[colToCheck];//get the range object where you want to search from
    
                                       resultRange = colRange.Find(
    
                                                       What: SearchNo,
    
                                                       LookIn: Excel.XlFindLookIn.xlValues,
    
                                                       LookAt: Excel.XlLookAt.xlPart,
    
                                                       SearchOrder: Excel.XlSearchOrder.xlByRows,
    
                                                       SearchDirection: Excel.XlSearchDirection.xlNext
    
                                       );


    Friday, January 4, 2019 7:53 AM

Answers

  • Note how the result of your search is an Excel.Range object. This is a complex object that you can examine at runtime using the debugger. Somewhere within its content there is a property named Row that represents the first row within the range. This is the value that you want to obtain. Just use the debugger to see where it is nested, and then write the corresponding expression such as

    int row = resultRange.whatever.Row

    where "whatever" is the nesting that you found using the debugger. It may be as simple as resultRange.Row, but rather than trying to guess or spend a lot of time trying to find it in the documentation it is much easier to just set a breakpoint and then examine resultRange using the debugger.

    • Marked as answer by Gani tpt Friday, January 4, 2019 9:48 AM
    Friday, January 4, 2019 8:07 AM
    Moderator

All replies

  • Note how the result of your search is an Excel.Range object. This is a complex object that you can examine at runtime using the debugger. Somewhere within its content there is a property named Row that represents the first row within the range. This is the value that you want to obtain. Just use the debugger to see where it is nested, and then write the corresponding expression such as

    int row = resultRange.whatever.Row

    where "whatever" is the nesting that you found using the debugger. It may be as simple as resultRange.Row, but rather than trying to guess or spend a lot of time trying to find it in the documentation it is much easier to just set a breakpoint and then examine resultRange using the debugger.

    • Marked as answer by Gani tpt Friday, January 4, 2019 9:48 AM
    Friday, January 4, 2019 8:07 AM
    Moderator
  • Excellent..done!...

    It is,

    int FindRow = resultRange.Row;

    Friday, January 4, 2019 9:48 AM