ExcelCompare.vbs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
5/17/2009 11:42 PM
'######################################################### 'ExcelCompare.vbs 'Author: Vishnu Agrawal '######################################################### option explicit dim dim dim dim dim dim
args ExcelFilePath1, ExcelFilePath2 fso, objExcel objWorkbook1, objWorkbook2 objWorksheet1, objWorksheet2 cell, DiffCount
args = WScript.Arguments.Count If args < 2 then WScript.Echo "usage: ExcelCompare.vbs <ExcelFile1> <ExcelFile2>" WScript.Quit end If ExcelFilePath1 = WScript.Arguments.Item(0) ExcelFilePath2 = WScript.Arguments.Item(1) Set fso = CreateObject("Scripting.FileSystemObject") If (fso.FileExists(ExcelFilePath1) = false )Then WScript.Echo ExcelFilePath1 & " doesn't exist." WScript.Quit End If If (fso.FileExists(ExcelFilePath2) = false )Then WScript.Echo ExcelFilePath2 & " doesn't exist." WScript.Quit End If Set objExcel = CreateObject("Excel.Application") objExcel.Visible = false Set objWorkbook1 = objExcel.Workbooks.Open(ExcelFilePath1) Set objWorkbook2 = objExcel.Workbooks.Open(ExcelFilePath2) Set objWorksheet1= objWorkbook1.Worksheets(1) Set objWorksheet2= objWorkbook2.Worksheets(1) DiffCount = 0
For Each cell In objWorksheet1.UsedRange On Error Resume Next If cell.Value <> objWorksheet2.Range(cell.Address).Value Then WScript.Echo cell.Address & " : " & cell.Value & " (" & objWorksheet2.Range(cell.Address) 'Highlights in green color if any changes in cells (for the first file) cell.Interior.ColorIndex = 4 'Highlights the same cell in the Second file objWorksheet2.range(cell.Address).interior.colorIndex = 4 DiffCount = DiffCount + 1 Else cell.Interior.ColorIndex = 0 End If Next WScript.Echo DiffCount & " Cells found." WScript.Echo "Excel Comparison Finished ... " WScript.Echo WScript.Echo "Excel files have been highlighted with green color to show the differences." objExcel.displayAlerts = False objExcel.Save objExcel.Quit set objExcel=nothing Page 1 of 1