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
'**********************************************************
'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
相关文章推荐
- If you whant to download SIS, JAR and JAD file from WAP you have to upload file (in the same catalog) with this: [小糊涂的灵感]
- In C# how to find the mime type of a file
- Find the difference between two days in days, months and years
- What is the difference between the ways to implement inheritance in javascript.
- How to avoid Excel from prompting "The file you are trying to open, , is in a different format than specified by the file extension"? (转)
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- How can I force Python's file.write() to use the same newline format in Windows as in Linux (“\r\n”
- What are some of the differences between using recursion to solve a problem versus using iteration?
- Unable to get the project file from the Web Server(in .NET 2005 IDE)
- How to Find the Self Service Related File Location and Versions
- Unable to locate JAR/zip in file system as specified by the driver definition: mysql-connector-java-
- How to make two <div>…</div> in the same row?
- 错误"Unable to find manifest signing certificate in the certificat...
- Upload Data from Excel File in ABAP using TEXT_CONVERT_XLS_TO_SAP
- the difference between LAR, and the Bisquare Remain Robust in regression —Curve Fitting Tool
- What is the difference between _tmain() and main() in C++?
- What is the difference between @staticmethod and @classmethod in Python?
- Warning: Unable to determine the number of frames in this file
- C#_错误&quot;Unable to find manifest signing certificate in the certificate store&quot;
- The report is used to find enhancement and badi in sap.