Thursday 15 May, 2008

Comparing two Excel Files

1.
Create 2 excel file test and test1 and 1 columns FirstSheetColumn. It checks first columns data only.

TrueOrFalse = "Same"
DataTable.Import("C:\test.xls")
FirstTableCount = DataTable.GetRowCount
DataTable.Import("C:\test1.xls")
SecondTableCount = DataTable.GetRowCount

if (FirstTableCount <> SecondTableCount) then
msgbox "Both files are not Equal"
else
For I = 1 To FirstTableCount
DataTable.Import("C:\test.xls")
Datatable.SetCurrentRow(I)
value1 = DataTable.Value("FirstSheetColumn")
DataTable.Import("C:\test1.xls")
Datatable.SetCurrentRow(I)
value2 = DataTable.Value("SecondSheetColumn")
if(value1 <> value2) then
TrueOrFalse = "Different"
Exit For
end if
Next
end if
msgbox(TrueOrFalse)

Set ExpObjectExcelApp = CreateObject("Excel.Application")
ExpObjectExcelApp.Workbooks.open ("C:/Test1.xls")
ExpSheet = "Sheet1"

2.
Set ActObjectExcelApp = CreateObject("Excel.Application")
ActObjectExcelApp.Workbooks.open ("C:/Test2.xls")
ActSheet = "Sheet1"

Set ExpectedDataSheet = ExpObjectExcelApp.Worksheets(1).UsedRange
Set ActualDataSheet = ActObjectExcelApp.Worksheets(1).UsedRange

ExpObjectExcelApp.Worksheets(1).UsedRange.Font.Color = vbBlack
ActObjectExcelApp.Worksheets(1).UsedRange.Font.Color = vbBlack

For Each Cell In ExpectedDataSheet.Cells
RowIndex = Cell.Row
ColumnIndex = Cell.Column
If Cell.Value <> ActualDataSheet.Cells(RowIndex, ColumnIndex).Value Then
Cell.Font.Color = vbRed
ActualDataSheet.Cells(RowIndex, ColumnIndex).Font.Color = vbRed
Reporter.ReportEvent micFail,"miss match", "row = " & RowIndex &" Column = "& ColumnIndex
Else
Cell.Font.Color = vbGreen
ActualDataSheet.Cells(RowIndex, ColumnIndex).Font.Color = vbGreen
End If
Next

ExpObjectExcelApp.ActiveWorkbook.Save
ActObjectExcelApp.ActiveWorkbook.Save

ExpObjectExcelApp.Quit
ActObjectExcelApp.Quit

4 comments:

Anonymous said...

Hi Jitesh,

Do you know how we can set the cell color using DataTable methods?

-Sonya

Anonymous said...

entrant mnnksx liking urged drafts mode cooked symbols unimark spirit remarkably
semelokertes marchimundui

Anonymous said...

Vеry nicе post. I simply ѕtumbled upon youг weblοg and ωіshеd to say that I havе
reallу loved surfing аrounԁ your wеblog poѕts.
After аll I will bе ѕubscribіng to yоur rss feed and I hoрe you
wrіte οnce more vеry soon!

Негe is my blоg pοst; http://Cadetech.cl/wiki/index.php?title=User:RossJnd

Anonymous said...

Superb website you have here but I was curious if you knew of any
forums that cover the same topics talked about in this article?
I'd really like to be a part of online community where I can get suggestions from other experienced individuals that share the same interest. If you have any recommendations, please let me know. Thank you!

Also visit my weblog - peanis enlargement