Scott Stewart

Scott Stewart

  • 1.5k
  • 193
  • 18.3k

Excel Interop Problems

Jan 7 2025 3:41 PM

C#, .Net 8.0, Microsoft.Office.Interop.Excel (15.0.4795.1001)

I am trying to get the last row of a Worksheet using this line of code
int lastRow = ExcelWorksheet.Cells[ExcelWorksheet.Rows.Count, 5].End(XlDirection.xlUp).Row;
I have used this code in other applications and I have no problems with it. Today, after successfully stepping through the code in a unit test, my app will no longer build. The red squiggly appeared under the word 'End' and the error is CS1061 - 'object' does not contain a definition for 'End'...

It also decided to find a problem with value in the following lines claiming the same error.

result.Name = ExcelWorksheet.Cells[i, 5].Value;
result.Value = ExcelWorksheet.Cells[i, 6].Value;
result.Units = ExcelWorksheet.Cells[i, 7].Value;
As I said, I stepped through this code successfully just a while ago. I told CoPilot to write a unit test to test this function and it wrote a long test using Moq. I pasted it in my test class and it generated the errors. I deleted the Test Method and the errors remain. I asked Co Pilot to fix the errors. It changed the code to break down the line to two lines, one to return the Range and one to get the Row of the range. That failed and when I told CoPilot it failed, it gave me a new line of code, which was the line of code I originally had. 
Something else has nbeen modified and I can't find what. I am hoping someone has some experience with this sort of issue that can shed some light on this. 


Answers (1)