简体   繁体   中英

excel.Range.Find returns wrong value C#

I want to write an application iterating over a text file with semicolon separated content and search for every value in a specific column in another Excel file.

I am able to open the text file within Excel and it is opened the right way (I made the app visible and checked if it was formatted correctly). Now if I iterate over the text table and use the Excel.Range.Find method, it always returns the wrong row.

    Microsoft.Office.Interop.Excel.Range current_find = usedRange_2.Cells[3].Find((String)(row.Cells[7]).Value2
Microsoft.Office.Interop.Excel.Range first_find = null

//for each row in tableOftextFile
if (first_find == null && current_find!=null)
    {
          first_find = current_find;
    }
do
    {
    if (((String)current_find.Cells[3].Value2).Contains((String)row.Cells[7].Value2))
        {
          //this part is never reached somehow.
          break;
        }
     else
    {
         current_find = usedRange_2.FindNext(current_find);
    }
   } while (current_find!=first_find && current_find!=null);

Everything in my application works just fine so there should be no problems with the workbooks or the sheets or whatsoever.

The Find operation MUST find something because the values exist and I can search for them manually in the Excel app and get correct results but eg if I search for the value "40" and the result is returned to current_find , current_find.Cells[3].Value2 is "42" or something like this but NOT the result I expected.

If someone could please give me a hint or even a solution that would be great. Thank you :)

Sorry, I found my mistake and if someone else does the same, here is the answer:

I thought the Excel.Find method Returns the row with the match but it seems to return the Cell with the match. So i corrected the current_find.Cells[3].Value2 to current_find.Value2 and I am good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM