您的位置:首页 > Web前端

To find the differences between 2 Excel sheets in the same file

2009-03-04 09:10 609 查看
Option Explicit

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

'Purpose: To find the differences between 2 Excel sheets in the same file

' and mark:

' Green - match

' Red - don't mathch

' Black - items were not tested because in the expected results sheet, these cells do not contain data

'Inputs: strFilePath

' strFirstSheetName

' strSecondSheetName

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

Sub Compare2ExcelSheetsInTheSameFile(strFilePath, strFirstSheetName, strSecondSheetName)

Dim objExcelApplication

Dim rngActualSheetData 'Will hold a MS Excel range object

Dim rngExpectedSheetData

Dim intRowIndex

Dim intColumnIndex

Dim intAreDifferent

'1 - there are no diferences between the sheets, 0 - the sheets are different

intAreDifferent = 1

Set objExcelApplication = CreateObject("Excel.Application")

objExcelApplication.Workbooks.open (strFilePath)

Set rngActualSheetData = objExcelApplication.Worksheets(strFirstSheetName).UsedRange

Set rngExpectedSheetData = objExcelApplication.Worksheets(strSecondSheetName).UsedRange

'Initiallization of the sheets so that it will be possible to understand what went wrong

objExcelApplication.Worksheets(strFirstSheetName).UsedRange.Font.Color = vbBlack

objExcelApplication.Worksheets(strSecondSheetName).UsedRange.Font.Color = vbBlack

For Each Cell In rngActualSheetData.Cells

intRowIndex = Cell.Row

intColumnIndex = Cell.Column

If Cell.Value <> rngExpectedSheetData.Cells(intRowIndex, intColumnIndex).Value Then

Cell.Font.Color = vbRed

rngExpectedSheetData.Cells(intRowIndex, intColumnIndex).Font.Color = vbRed

intAreDifferent = 1

Else

Cell.Font.Color = vbGreen

rngExpectedSheetData.Cells(intRowIndex, intColumnIndex).Font.Color = vbGreen

End If

Next

If intAreDifferent = 0 Then

Reporter.ReportEvent 1, "Compare Excel Sheets in file: " & strFilePath, "There are differences between the actual and expected results, see Excel file for details"

End If

objExcelApplication.ActiveWorkbook.Save

objExcelApplication.Quit

Set objExcelApplication = Nothing

End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐