Sunday, March 29, 2009

QTP Script 33 - How to compare two excel sheets of the same excel workbook and highlight the cells with different values in the first sheet?

Excel object is being created here
Set Exl_Obj = CreateObject("Excel.Application")
Exl_Obj.Visible = True

File sac1 is being opened
Set WB_Obj_1= Exl_Obj.Workbooks.Open("C:\sac1.xls")

Cells in the first and second sheet of sac1 are compared
Set WS_Obj_1= WB_Obj_1.Worksheets(1)
Set WS_Obj_2= WB_Obj_1.Worksheets(2)
   For Each cell In WS_Obj_1.UsedRange
If cell.Value <> WS_Obj_2.Range(cell.Address).Value Then
cell.Interior.ColorIndex = 6
Else
cell.Interior.ColorIndex = 0
End If
Next
Exl_Obj.workbooks("sac1.xls").save
Exl_Obj.workbooks("sac1.xls").close
Exl_Obj.Application.Quit
set Exl_Obj=nothing