none
Unable to read worksheet used range while comparing two work sheets RRS feed

  • Question

  • I am comparing two work sheets and finding mismatching cell in the difference.

    My both excel sheets are reading properly. but, while checking the cell in both sheets, it always shows "0" zero values.

    Each sheet range starts from A10 to IJ10)

    column starts range A to IJ Column in both sheets

    Row Starts range 10th row in both sheets.

    what is the problem in my source code.? 

    string strRangeToCheck;
    strRangeToCheck = "A10:IV10";
    lastLine = Math.Max(
               XLWS1.Range[strRangeToCheck].Rows.Count,
               XLWS2.Range[strRangeToCheck].Rows.Count);
    
               for (int row = 1; row <= lastLine; row++)
               {
                   // maximum column 
                   int lastCol = Math.Max(
                   XLWS1.Range[strRangeToCheck].Columns.Count,
                   XLWS2.Range[strRangeToCheck].Columns.Count);
                   for (int column = 1; column <= lastCol; column++)
                   {
            	         double? Currval = new double?();
    	                 double? Prevval = new double?();
                             //int? value =  new double?();
                             Currval = Convert.ToDouble(XLWS1.Cells[row, column].Value);
                             Prevval = Convert.ToDouble(XLWS2.Cells[row, column].Value);
                             // Check for if the object is null.
                             if ((Currval == null) || (Prevval == null))
                             {
                                               // Your code goes here.
                             }
                             else
                             {
                                  if (Currval != Prevval)
                                  {
                                        ((Excel.Range)XLWS1.Cells[row, column]).Interior.Color = 255;
                                        ((Excel.Range)XLWS2.Cells[row, column]).Interior.Color = 5296274;
                                   }
                             }
                    }
                }

    Some where i did mistake to read Rows and columns...where is that...?


    Wednesday, January 2, 2019 6:13 AM

Answers

  • Maybe you should use these loops:

       var r = XLWS1.Range[strRangeToCheck];

       for( int row = r.Row; row < r.Row + lastLine; row++)

    and

      for( int column = r.Column; column < r.Column + lastCol; column++)

    • Marked as answer by Gani tpt Thursday, January 3, 2019 6:19 AM
    Wednesday, January 2, 2019 6:27 AM