Forum Discussion

kandy1984's avatar
kandy1984
Contributor
11 years ago

Comparing sheets without the first 2 lines in the excel sheet

Hi,



I need to compare part of an excel sheet to another excel sheet. In the attached image, i do not want the first 2 rows (row 1 and 2 to be compared) but the rest of them to be compared.



I found a piece of code which does the comparing of the whole excel. How do i modify to just compare what i need.




 'Loop to identify the differences per worksheet


  For i = 1 To strCount 


  


    'Getting the row and column count of the first worksheet 


    Set objWorksheet1 = objSpread1.Worksheets(i)


    With objWorksheet1.UsedRange


      x1 = .Rows.Count


      y1 = .Columns.Count


    End With


    For tOff = 1 to x1 


      If (objWorksheet1.Cells(tOff,1) <> "") Then


        fOffset = tOff


        Exit For


      End If


    Next


    


    'Getting the row and column count of the the second worksheet 


    Set objWorksheet2 = objSpread2.Worksheets(i)


    With objWorksheet2.UsedRange


      x2 = .Rows.Count


      y2 = .Columns.Count


    End With


    maxR = x1


    maxC = y1


    If maxR < x2 Then


      maxR = x2


    End If


    If maxC < y2 Then


      maxC = y2


    End If


    


    'Loop to find the differences between the two files (cell by cell)


    cf1 = ""


    cf2 = ""


    For c = 1 To maxC 


      For r = 1 To (maxR+fOffset) 


        On Error Resume Next


          cf1 = LTrim(RTrim(objWorksheet1.Cells(r, c).Value))


          cf2 = LTrim(RTrim(objWorksheet2.Cells(r, c).Value))


          PDiffCount = DiffCount


        If Isnumeric(cf1) And Isnumeric(cf2) Then


          If Abs(cf1-cf2) >= 1 Then


            DiffCount = DiffCount+1


          End If


          Else


            If cf1 <> cf2 Then


            DiffCount = DiffCount+1


          End If


        End If


        If not IsNull(resfile) Then 


          If DiffCount >= (PDiffCount+1) Then


            objWorksheet1.Cells(r,c).Interior.ColorIndex = 3


            objWorksheet2.Cells(r,c).Interior.ColorIndex = 3


            resWorkSheet.Cells(resOffSet, 1) = objSpread1.Worksheets(i).Name


            resWorkSheet.Cells(resOffSet, 2).Formula = "=Address("&r&", "&c&", 4)"


            resWorkSheet.Cells(resOffSet, 3) = objWorksheet1.Cells(r, c).Value


            resWorkSheet.Cells(resOffSet, 4) = objWorksheet2.Cells(r, c).Value


            resOffSet = resOffSet + 1


          End If


        End If


        cf1 = ""


        cf2 = ""


      Next


    Next


  Next


  If DiffCount=0 Then


    sMsg = "No mismatches found."


  Else


    resBook.SaveAs resultFile


    sMsg =  DiffCount & " items mismatches. " & vbLF & "The result file available at : " & resultFile


  End If





Thanks for helping...

Sudha

1 Reply

  • gid_216's avatar
    gid_216
    Frequent Contributor
    Hi,



    the number of rows to skip or the number of row to start can be passed as a parameter to the function responsible for comparision. if there is no separate function, create a function specific to this comparision and use that in you framework.



    E.g.

     


     


    Function fn_ExcelCompare(strFileName, strSheetName, intRowToSkip)


     


    'For Excel COM


    '*******************************************************************


    'Create Excel object, Open workbook, worksheet


    'Get the Used range row and column.


     


    For intCounter = intRowtoSkip + 1 To intRowCount


                  '''TODO Script


    Next


     


    '*******************************************************************


    'For DDT ExcelDriver


    '*******************************************************************


     


    'Open ExcelDriver


     


    objExcelDriver.First 


    While intRowToSkip > 0 'If the number of rows to skip includes first row of excel then replace 0 with 1


    objExcelDriver.Next


    intRowToSkip = intRowToSkip - 1


    Wend


    'Start comparision using DDT driver


    End Function


     


    This is to compare a single file with a reference. As you are comparing to excels, in the function, create two object for each file and compare. The parameter will get change in this case. Parameters will be two excel files and two sheet names (it can be one sheetname if both the file contains same sheet)


     


    Hope this will help.