kandy1984
11 years agoContributor
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
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