Home » excel » excel – QTP/UFT: Comparing two excelsheets easy and performant using VB

excel – QTP/UFT: Comparing two excelsheets easy and performant using VB

Posted by: admin May 14, 2020 Leave a comment


I want to compare 2 Excel-Sheets in VB.
I know how to open these sheets in QTP/UFT
I know how to get the ranges of all cells.
But I do not know a performant way to compare these 2 sheets (ranges).

If a Value in 2nd Sheet is different to the corresponding value in first sheet it shall be inserted in the corresponding cell of 2nd Sheet.
I can parse all cells in serial to compare with the corresponding cell, but this is not very performant.

I want to compare it in following way:

If not (sheet1.range() equals sheet2.range()) then
  if not (Sheet1.range.Row equals sheet2.range.row) then
    if not (Sheet1.range.Row.cells equals Sheet2.range.Row.cells) then
      Sheet2.range.Row.cells.value = Sheet1.range.Row.cells.value

All Values could be in different rows and cells but there are corresponding ID in first Column and in the Header of columns.
So I have to parse for the correct row-ID in both sheets and the corresponding Column in both column headers.
I do not want to use a formula in the sheet, because I have to do the comparison in programmatical way.

Could someone please give me a hint if there is a function, method or property in Excel I could use?
Thanks in advance

How to&Answers:

Hope It May Help You.
It compares and if in second sheet any change from first sheet it will update the cell value and highlight it in yellow color.

Set objMyExcel = CreateObject(“Excel.Application”)
objExcel.Visible = False

Set objMyDataExcel1= objMyExcel.Workbooks.Open(“C:\Data1.xls”)
Set objMyDataExcel2= objMyExcel.Workbooks.Open(“C:\Data2.xls”)

Set objMyWorksheet1= objMyDataExcel1.Worksheets(1)
Set objMyWorksheet2= objMyDataExcel2.Worksheets(1)

For Each cell In objMyWorksheet1.UsedRange

   If cell.Value <> objMyWorksheet2.Range(cell.Address).Value Then

       objMyWorksheet2.Range(cell.Address).Value = cell.Value
       cell.Interior.ColorIndex = 6


       cell.Interior.ColorIndex = 0

   End If


Set objMyExcel = Nothing

if you want to see the excel on execution time then set

objExcel.Visible = True