Forum Discussion
shankar_r
9 years agoCommunity Hero
Hi,
I was doing the same kind of validation long back using Excel VBA.In Test Complete, I used to compare the excel file column and rows using below code. It was working fine for me. Maybe you could try this.
Function CompareExcelCells()
Dim excelObject
Dim WB_A,WB_B,WS_A,WS_B
Dim path_A,path_B
Dim A_RowCount,iR
path_A = "D:\************************\A.xlsx"
path_B = "D:\************************\B.xlsx"
Set excelObject = Sys.OleObject("Excel.Application")
'Open Excel files
Set WB_A = excelObject.Workbooks.Open(path_A)
Set WB_B = excelObject.Workbooks.Open(path_B)
Set WS_A = WB_A.Sheets("Sheet1")
Set WS_B = WB_B.Sheets("Sheet1")
A_RowCount = WS_A.UsedRange.Rows.Count
For iR = 1 To A_RowCount
If(WS_A.Cells(iR,1).Value = WS_B.Cells(iR,1).Value) Then
If(WS_A.Cells(iR,4).Value = WS_B.Cells(iR,5).Value) Then
Log.Message("Value matched " & WS_A.Cells(iR,1).Value)
WS_A.Cells(iR,1).Interior.ColorIndex = 0
Else
WS_A.Cells(iR,1).Interior.ColorIndex = 3
End If
Else
WS_A.Cells(iR,1).Interior.ColorIndex = 3
End If
Next
WB_A.Save()
WB_B.Save()
excelObject.Quit()
Set excelObject = Nothing
End Function
msal4434
9 years agoOccasional Contributor
Hi Shankar,
Thank you for your solution.
Need one more solution.
-Read A1(Column1) from Sheet1 and find A1(Column1) in Sheet2.
If there is a match than Read G1(Column 7) from Sheet 1 and F1(Column 13) in Sheet2 and check if they match or not?
Thanks and Regards
Sal
- shankar_r9 years agoCommunity Hero
In this case, you just change the Column Number in the below highlighted code.
If(WS_A.Cells(iR,1).Value = WS_B.Cells(iR,1).Value) Then If(WS_A.Cells(iR,4).Value = WS_B.Cells(iR,5).Value) Then Log.Message("Value matched " & WS_A.Cells(iR,1).Value) WS_A.Cells(iR,1).Interior.ColorIndex = 0 Else WS_A.Cells(iR,1).Interior.ColorIndex = 3 End If Else WS_A.Cells(iR,1).Interior.ColorIndex = 3 End If