VBScript: Compare 2 Excel Files

by Anshoo Arora ON March 22, 2010 · Posted In All, QTP, VBScript · 75 comments

This post shows a quick and easy way to compare 2 Excel files. I know there are several code snippets on forums that show how to compare do this, so you may ask the following question: how is this technique any different? What makes this technique different is performance. I know there may be faster ways to achieve this, but after much experiment, this was the one that continuously worked at the best speed in comparison to other methods.

The main concept of this technique is performing all validations through a range object, instead of traversing all cells of an Excel sheet (which takes a little longer). Below is the skeleton of how the class is arranged, with documentation. Please use one of the download links above to download this class. There is a usage example shown at the end of this article.

Class clsExcelComparer
Class clsComparer
	'[--- Region Private Variables Start ---]

	Private oExcel		'Excel.Application
	Private arrRangeUno	'Range.Value (array) of the Primary Excel spreadsheet
	Private arrRangeDos	'Range.Value (array) of the Secondary Excecl spreadsheet
	Private oDict		'Scripting.Dictionary containing unmatched cells

	'[--- Region Private Variables End ---]

 
	'[--- Region Public Variables Start ---]

	Public Operation	'0: Only Compare   1: Compare & Highlight Differences

	'[--- Region Public Variables End ---]

 
	'--------------------------------------------------------
	' Name: Function Compare [Public]
	'
	' Remarks: N/A
	'
	' Purpose: Compares differences between 2 Excel Spreadsheets
	'	
	' Arguments:
	'	sWorkBookUno: Primary Excel WorkBook (with complete path)
	'	vSheetUno: Primary Excel Spreadsheet Name
	'	sWorkBookDos: Secondary Excel WorkBook (with complete path)
	'	vSheetDos: Secondary Excel Spreadsheet Name
	'
	' Return: Boolean
	'
	' Author: Anshoo Arora, Relevant Codes
	'
	' Date: 03/17/2010
	'
	' References: N/A
	'--------------------------------------------------------
	Public Function Compare(sWorkBookUno, vSheetUno, sWorkBookDos, vSheetDos)
 
	End Function
 
	'--------------------------------------------------------
	' Name: Function CellsFound [Private]
	'
	' Remarks: N/A
	'
	' Purpose: Finds the dissimilar cells between 2 sheets
	'	
	' Arguments: N/a
	'
	' Return: Integer
	'
	' Author: Anshoo Arora, Relevant Codes
	'
	' Date: 03/17/2010
	'
	' References: N/A
	'--------------------------------------------------------
	Private Function CellsFound()
 
	End Function
 
	'--------------------------------------------------------
	' Name: Sub Class_Terminate [Private]
	'
	' Remarks: N/A
	'
	' Purpose: Disposes the Excel.Application object
	'	
	' Arguments: N/A
	'
	' Author: Anshoo Arora, Relevant Codes
	'
	' Date: 03/17/2010
	'
	' References: N/A
	'--------------------------------------------------------
	Private Sub Class_Terminate()
 
	End Sub
 
End Class
 
'--------------------------------------------------------
' Name: Function CompareExcelSheets
'
' Remarks: N/A
'
' Purpose: Constructor for Class clsComparer
'	
' Arguments:
'	sWorkBookUno: Primary Excel WorkBook (with complete path)
'	vSheetUno: Primary Excel Spreadsheet Name
'	sWorkBookDos: Secondary Excel WorkBook (with complete path)
'	vSheetDos: Secondary Excel Spreadsheet Name
'	Operation: 0: Compare Only   1: Compare & Highlight Differences
'
' Return: Boolean
'
' Author: Anshoo Arora, Relevant Codes
'
' Date: 03/17/2010
'
' References: N/A
'--------------------------------------------------------
Function CompareExcelSheets(sWorkBookUno, vSheetUno, sWorkBookDos, vSheetDos, Operation)
 
End Function

Download clsComparer (772) | View clsComparer (724)

Usage (Example)

Consider the following 2 Excel Sheets, with a few dissimilar cells:

Sheet #1 Original

Sheet #1 Original

Sheet #2 Original

Sheet #2 Original

There are 2 main operations that can be done through our code. 1. We can simply check if both Excel Sheets are similar and 2. we can check as well as highlight the differences between the two. To only compare the 2 sheets, without highlighting the differences the Operation argument will have a value of 0. On the other hand, to compare as well as highlight the values, the Operation argument will have a value of 1. Let’s first consider a case where the 2 sheets are only to be compared:

Compare Only
'Only Compare:
MsgBox CompareExcelSheets("C:\Test1.xls", "Sheet1", "C:\Test2.xls", "Sheet1", 0)

Because the 2 sheets are not similar, the following will be the output upon execution of the above statement:

Compare Only

Compare Only

Also, the 2 sheets will remain unchanged.

When changing the value of Operation to 1, we will notice that, not only does the code compare the 2 sheets, but also highlights the differences.

Compare and Highlight Differences
'Compare and highlight differences:
MsgBox CompareExcelSheets("C:\Test1.xls", "Sheet1", "C:\Test2.xls", "Sheet1", 1)

Upon execution of the above statement, the following 2 sheets will be highlighed in areas where they differ:

Sheet #1 With Highlighted Differences

Sheet #1 With Highlighted Differences

Sheet #2 with Highlighted Differences

Sheet #2 with Highlighted Differences

Limitations

1. This code will work with Excel Sheets with the same number of rows and columns in each (like ranges).

Update: RelevantCodes.Comparer COM DLL

Download RelevantCodes.Comparer

Snapshot of the assembly can be found here.

To register the assembly to your PC, the following code can be used:

RegAsm C:\RelevantCodes.dll /codebase

or

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe C:\RelevantCodes.dll /codebase

Once done, the code can be used in QTP like this:

Set oCompare = CreateObject("RelevantCodes.Comparer")

oCompare. should give you the proper intellisense to use all methods. The assembly compares the following items:

  1. Arrays
  2. Excel Sheets
  3. Images
  4. Integers
  5. Strings
  6. TextFiles

I hope you find this useful :)

Subscribe to Relevant Codes (by Anshoo Arora)

Hello! We're always posting interesting articles on Relevant Codes. Why not subscribe so you don't miss out?

Leave a Comment

{ 73 comments… read them below or add one }

Ameena February 19, 2012 at 5:21 pm

Anshoo,

How formula can be applied to a sheet full of numbers
=(A1*5.5%)+A1

Regards

Ameena

Reply

Ameena February 19, 2012 at 5:21 pm

QTP / VB Script for this

Reply

Tara Glaze February 4, 2012 at 9:13 am

I loved your blog.Much thanks again. Really Great.

Reply

pw November 24, 2011 at 1:40 am

How to compare 2 files and generate a report with specific contents using C# code

Reply

Anonymous October 25, 2011 at 1:25 pm

Please e-mail me a code for a XLS compare program. I need to compare two Excel files.
Thank you. Emily

Reply

Mamatha October 10, 2011 at 7:19 am

Hello Anshoo,
can you explain me how to sort excel columns data either ascending or descening order using vb script?

Const Xlascending=1

Dim objExcel

set objExcel= createobject(“Excel.application”)
objExcel.visible= True
set myworkbook= objExcel.workbooks.open(“C:\Documents and Settings\RAV\Desktop\QTP\dataforsort.xls”)
set mysheet= myworkbook.worksheets(1)
set objrange= mysheet.usedrange
set objrange2=objExcel.Range(“A1″)

objrange.sort objrange2, Xlascending

objExcel.activeworkbook.save
objExcel.quit
set objExcel=nothing

i tried this one but its not working

waiting for your reply
thanks in advance

Reply

Prashanth Pai October 4, 2011 at 9:01 am

Hello Anshoo,
I am automating an Excel application using QTP11.
How to realize selecting menu and items of Excel window in QTP?
Upon spying, the entire menu is recognized as WinObject.
Please help.

Reply

vinay Singh October 3, 2011 at 7:54 am

Hi Folks,

I need help in rearranging a excel sheet
I am Downloading a report file from one of my software Here I am Getting data of one component enclosed between two Brackets (either () or []) and there are 100 of such components now I want a script which can arrange the Data of components in row and column format….sample data added below
Please help

Sample Data:
[
U3
DFN10
TPS54060DGQ

]
[
SW2
SW7MM
50-0014-00

]
[
R206
0603
1K

]
[
R205
0603
4.7K

]

Reply

Felix September 22, 2011 at 11:29 pm

Hi Anshoo,

I tied your class and found it working very good for files which got almost equal data.
What you will suggest in case when filse are contain similar data, but got different order in columns or in rows?

Thanks,
Felix.

Reply

Adwita September 14, 2011 at 3:17 am

Hi Anshoo,

I would like to use Excel formula to increase the excel cell values by 3.5 % on existing values for all the cells.

=A1*(1+0.035). This is working fine in Excel but how this can be done from QTP. How can i apply the percentage using QTP on excel sheet.

Thanks in advance
Regards

Adwita

Reply

Anshoo Arora September 24, 2011 at 8:28 pm

Adwita: You can do something like:

xlsSheet.Cells(iRow, vColumn).Value = CInt(xlsSheet.Cells(iRow, vColumn).Value) * 1.035

Reply

Cash Coach August 29, 2011 at 8:16 am

I am trying to figure out how many items I need to order for every item listed. I have a sheet of items (rows) where for each row there may be a quantity needed or not. For each row that has an amount needed, I need to create another worksheet with ONLY the rows where there is an actual requirement with the quantity needed, item name and state. Here is sample data of the inventory in Worksheet A:

Name State Qty
Apples WA 5
Oranges FL
Car MI 8
Furniture VA 3
Corn IN

Notice that the Qty is null in many of the rows. Desired output on Worksheet B:

Name State Qty
Apples WA 5
Car MI 8
Furniture VA 3

So the output does not include the empty qty records. I’ve tried various row formulas and others and am just having a hard time. I’d prefer to do this with a formula rather than VBA macro or such and not using a filter. Any assistance would be appreciated.

Reply

Saran April 28, 2011 at 5:27 am

Hi,

How to compare 2 excel sheet using vlookup function.

Reply

Mahesh April 14, 2011 at 1:26 am

Hi, When I execute the script,I got the pop up saying “bDisplayExceptions:False” and unmatched part didnt show in Red Colour.
Please let me know what is the reason?
my code is like this

public bool GridData_Comparison(string ActualSheetPath,int sheetnumber1, string ExpectedSheetPath,int sheetnumber2)
{
Comparer comparer = new Comparer();
string ActualSheetIndex = “Sheet” + sheetnumber1;
string ExpectedSheetIndex = “Sheet” + sheetnumber2;
return comparer.ExcelSheets(ActualSheetPath,ActualSheetIndex,ExpectedSheetPath,ExpectedSheetIndex,true);
}
msgbox obj.GridData_Comparison(“H:\xxx.xls”,1,”H:\yyyy.xls”,1). Can you please give the code of Excel Comparison

Reply

Anonymous March 14, 2011 at 10:10 pm

Dim myCellValue1(15000), myCellValue2(15000), z
set fso=CreateObject(“Scripting.FileSystemObject”)
set fout = fso.CreateTextFile(“C:\fileComp.txt”, true)

‘First one
Set excelApp1 = CreateObject(“Excel.Application”)
excelApp1.Workbooks.Open “C:\A.xls”
set fso=CreateObject(“Scripting.FileSystemObject”)
Set currentWorkSheet1 = excelApp1.ActiveWorkbook.Worksheets(1)
ccount1 = currentWorkSheet1.UsedRange.Columns.Count
rcount1 = currentWorkSheet1.UsedRange.Rows.Count

msgbox ccount1 & ” ” & rcount1 & ” ” & ccount2 & ” ” & rcount2
‘Second one
Set excelApp2 = CreateObject(“Excel.Application”)
excelApp2.Workbooks.Open “C:\B.xls”
Set currentWorkSheet2 = excelApp2.ActiveWorkbook.Worksheets(1)
ccount2 = currentWorkSheet2.UsedRange.Columns.Count
rcount2 = currentWorkSheet2.UsedRange.Rows.Count

‘Output
Set excelApp3 = CreateObject(“Excel.Application”)
excelApp3.Workbooks.Open “C:\result.xls”
Set currentWorkSheet3 = excelApp3.ActiveWorkbook.Worksheets(1)
msgbox ccount1 & ” ” & rcount1 & ” ” & ccount2 & ” ” & rcount2
‘navigating through first sheet
z=1
For i=2 to rcount1 ‘i=2
‘For i=2 to 7
For j=1 to 3
‘to get a value from a cell
myCellValue1(j)= currentWorkSheet1.cells(i,j)
‘ for each cell in first sheet
‘navigating through the second sheet to find a match
Next
Dim fname1,lname1, fnam2, lname2
fname1 = myCellValue1(1)
lname1 = myCellValue1(2)
‘ fout.writeline “1st ” & fname1 & ” ” & lname1

‘For l=1 to rcount2
For l=2 to rcount2 ‘l=2

For m = 1 to 3
myCellValue2(m)= currentWorkSheet2.cells(l,m)
Next
fname2 = myCellValue2(1)
lname2 = myCellValue2(2)
‘fout.writeline “2nd ” & fname2 & ” ” & lname2
if((fname1=fname2) or (lname1=lname2)) then

‘match found
‘now write to datatable saying that match is found

‘currentWorkSheet3.cells(i,1)=fname1
‘currentWorkSheet3.cells(i,2)=lname1
‘ For k=3 to 46
For k=3 to 3

If Not (myCellValue1(k)= myCellValue2(k)) Then
fout.writeline fname1 & ” ” & lname1 & ” mismatch”
currentWorkSheet3.cells(z,1) = fname1
currentWorkSheet3.cells(z,2) = lname1
currentWorkSheet3.cells(z,k) = “miss match”
z = z+1

end if

Next
Exit for
‘msgbox myCellValue1 & ” match found ” & myCellValue2
end if
Next
Next
fout.Close
excelApp3.Save
excelApp1.quit
excelApp2.quit
excelApp3.quit

Reply

Vijju September 27, 2010 at 12:38 am

When i run this on QTP 10.0 it is throwing this error.

Run Error:
Type mismatch: ‘UBound’

Function file: D:\clsComparer.cls.vbs
Line (127): ” iBoundsUno = UBound(arrRangeUno,2)”.

Also while attaching the functional library clsComparer.cls.vbs, it is showing this dialog message

Automatic Relative Path Conversion: (dialog box)

Do you want to add ‘D:’ to the search list in the Folders pane of the Options dialog box and convert the path ‘clsComparer.cls.vbs’ to a relative path?

Reply

Vijju October 22, 2010 at 8:02 pm

I got this error message while running it worked first time then it thrown the error message.

MsgBox CompareExcelSheets(“D:\Test1.xls”, “Sheet1″, “D:\Test2.xls”, “Sheet1″, 1)
MsgBox CompareExcelSheets(“D:\Test1.xls”, “Sheet2″, “D:\Test2.xls”, “Sheet2″, 1)

Run Error: Type mismatch: ‘UBound’

Function file: C:\clsComparer.cls.vbs\clsComparer.cls.vbs
Line (127): ” iBoundsUno = UBound(arrRangeUno, 2)”.

Reply

Vijju September 26, 2010 at 12:56 am

Hi Anshoo,

I’ve tested this in QTP 9.2 it was working fine, but when i tested this in QTP 10.0 trial version it did not work. I just attached the clsComparer.cls file, did not register anything. So what could be the reason it did not work in QTP 10.0

Many thanks

Vijju

Reply

Adwita September 21, 2010 at 12:11 am

Hello Anshoo,

how can i compare excel sheet with another excel sheet when the cells in both the sheets are in different cells. I want to compare A1 in one sheet to L2 in another sheet (Like this i want to compare few more values) of different excel file.

Reply

Archishu September 20, 2010 at 11:34 pm

Hey anshoo,

I would like to automate the following scenario, currently i’m doing this manually using excel. I would like to compare Sheet1 with values filled and apply this ex. formula = (A2*1.5%)+A2 and comparing it with Sheet1 in other excel file with original value. Its like 1.5 % more than the other one. And display the variances.

Reply

Rohan September 20, 2010 at 11:27 pm

Hi Anshoo,
How can we compare multiple excel sheets at once. For ex Sheet1 from ExcelFile1.xls to Sheet1 from ExcelFile2.xls and Sheet2 from ExcelFile1.xls to Sheet2 from ExcelFile2.xls

many thanks

Reply

Rohan September 26, 2010 at 1:06 am

Thanks anshoo this is also working

Reply

deepak September 19, 2010 at 11:35 pm

Hi Anshoo,

How to compare 2 excel sheet cells (One cell in one excel sheet to different cell in different sheet, For ex Compare A2 to I2 / B3 to I3) and display all the variances at the end.

Reply

deepak September 15, 2010 at 9:04 pm

Hi Anshoo,

Is there a way we can compare 2 excel sheets cell by cell and / compare by range / compare by sheet and display the discrepancies.

Reply

Anshoo Arora September 29, 2010 at 2:54 pm

Deepak,

Yes, there is. A range is nothing but a collection of values in all the cells – a 2D array. The comparison method will remain the same. Cell by cell comparison would involve running a loop for the used range. Comparison by sheet would be either of the 2.

Reply

Ansh Khandelwal August 30, 2010 at 10:36 pm

Hi Anshoo,

I have downloaded your vb script class file, but I am little weak in vb script.Just wanted to know how to use this code.Looking for your reply.

Thanks !!

Reply

Anshoo Arora September 2, 2010 at 7:44 am

Ansh,

You can use the code in the following 2 ways:

'Compare Only
MsgBox CompareExcelSheets("C:\Test1.xls", "Sheet1", "C:\Test2.xls", "Sheet1", 0)

and

'Compare and highlight differences:
MsgBox CompareExcelSheets("C:\Test1.xls", "Sheet1", "C:\Test2.xls", "Sheet1", 1)

Reply

AMIT SARAN August 10, 2010 at 6:46 am

Sorry Anshoo I was in hurry I didnt check extension of file it is .xlsx in my system it is working good. Thanks for the useful post.
And please post an article on Environment variable.

Reply

AMIT SARAN August 10, 2010 at 6:42 am

Sorry Anshoo I was in hurry I didnt checked extension of file it xlsx in my system it is working good

Reply

AMIT SARAN August 10, 2010 at 6:38 am

Hi Anshoo,

I used ur class but i m getting error Test1.xls couldn’t be found even I saved it appropriate location

Reply

Jai August 2, 2010 at 7:54 am

Can please tell me Automation Process start to end.

Reply

Jai August 2, 2010 at 7:26 am

Then I used regsvr32 c:\relevantcodes.dll command but I got an error DLLRegisterServer entry point not Found

Reply

Jai August 2, 2010 at 7:16 am

I used RegAsm command but Editor is showing “RegAsm is not recognised command”

Reply

Anshoo Arora August 3, 2010 at 9:47 am

Jai,

Use this instead:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe C:\RelevantCodes.dll /codebase

Ignore any warnings you get. I haven’t made the assembly strong named so you will see the error – but the types will be registered successfully. You cannot use regsvr32 as this is a .NET assembly. You will have to use RegAsm.

Reply

Jai August 2, 2010 at 5:59 am

Plz tell me where to write it

Reply

Jai July 17, 2010 at 2:27 pm

Hi Anshoo Hw r u dear Thanks for valuable posts. Actually I am new bird. Tell me How to register this dll u gave the steps but i dnt knw where to write it Plz help me out nd give me the steps

Reply

Anshoo Arora July 30, 2010 at 8:43 am

Jai,

You can register the DLL using the following code:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe C:\RelevantCodes.dll /codebase
'or
RegAsm C:\RelevantCodes.dll /codebase

Reply

Aparna July 16, 2010 at 5:36 am

Hi,

Your script is very good. I am just a beginner in vb. Please tell me where do I insert the class, function and how and what code do I insert in ThisWorkbook.

Reply

Anshoo Arora July 30, 2010 at 8:38 am

Aparna,

The entire class can be stored inside a library and associated with the test via test settings or associated dynamically via ExecuteFile.

Reply

Vamsi July 9, 2010 at 10:17 am

Hi Anshoo,
First of all Thank you very much for posting such a wonderful articles, you are a gift for all the automation testers across the world.
I have one tricky requriement in my project, where i need to compare the following contents in 2 Excel sheets
Sheet1: These all contents are in saperate cells of Sheet
Col1 Col2 Col3
Row1 1973/74 22 x 60′ 22
Row2 1974/75 25 x 60′ 25
Row3 1975/76 24 x 60′ 24
Row4 1976/77 25 x 60′ 25
Row5 1977/78 22 x 60′ 22
Sheet2: Now here all the contents in one particular cell
Col1
1973/74 – 22 x 60′ – 22 hours
1974/75 – 25 x 60′ – 25 hours
Row1 1975/76 – 24 x 60′ – 24 hours
1976/77 – 25 x 60′ – 25 hours
1977/78 – 22 x 60′ – 22 hours
It will be great if you can guide me of how to do this comparision
Thanks,
vamsi

Reply

Anshoo Arora July 14, 2010 at 2:40 pm

This is a little tricky, but if you can append the data from one sheet as string delimited by “-”, you can easily get the job done. For example:

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.WorkBooks.Open("TheFileName")
Set xlSheet1 = xlBook.WorkSheets("Sheet1")
Set xlSheet2 = xlBook.WorkSheets("Sheet2")

iCols1 = xlSheet1.UsedRange.Columns.Count
iRows1 = xlSheet1.UsedRange.Rows.Count
iRows2 = xlSheet2.UsedRange.Rows.Count

For iRow = 2 to iRows1
	For iCol = 1 to iCOls1
		sStr = sStr & xlSheet1.Rows(iRow).Columns(iCol).Value & " - "
	Next

	If xlSheet1.Rows(iRow).Columns(iCol).Value = sStr Then
		Reporter.ReportEvent "Match at Row " & iRow & " Column " & iCol, "Matched"
	End If
Next

xlBook.Close
xlApp.Quit

The above is a sample code. You may need to modify it to work as per your needs.

I hope this helps.

Reply

Manpreet July 5, 2010 at 7:23 am

How to use createobject(“adodb.connection”) to get the data from .xlsx (2007) excel sheet.
I am usinng the connection but not able to get the data kindly help me resolveing the issue .

Set conn = CreateObject(“ADODB.Connection”)
conn.ConnectionString = “Driver={Microsoft Excel Driver (*.xls)}; DBQ=” & sFileNameAndPath & “;”:conn.Open
Set objRS = CreateObject(“ADODB.Recordset”)
sSQL = “SELECT * FROM [" & sSheetName & "$] “

Reply

Anshoo Arora July 6, 2010 at 9:22 pm

Hi Manpreet,

Please see this article. It uses the same approach to pull data from Excel and works with both Excel 2003 and 2007.

Reply

Kumar July 5, 2010 at 2:44 am

Hi, the download link is giving 404 error. Can you check the link.

Reply

Anshoo Arora July 6, 2010 at 9:21 pm

Hi Kumar,

I have fixed the links.. there is something wrong with the software that I use to upload and track the number of downloads. It should be working now. Thanks for bringing this up. :)

Reply

Arun Kumar June 29, 2010 at 12:32 am

Hi Anoosh Arora,

I have tried this Excel compare script and it is working fine.

Can we highlight the differences in any other color other than red?

If yes, can you please help me in doing it?

Thanks,
Arun

Reply

Anshoo Arora July 6, 2010 at 12:22 pm

Sure. You can change the following lines of code to the color you want:

oSheetUno.Rows(iRow).Columns(iCol).Interior.ColorIndex = 3
oSheetDos.Rows(iRow).Columns(iCol).Interior.ColorIndex = 3

PS. Its not Anoosh. Its Anshoo. :)

Reply

Manpreet June 23, 2010 at 2:49 am

HI Anshoo,

Thanks for the help.

But what are methods or functions that are available in this DLL.
http://relevantcodes.com/Articles/RelevantCodes.Comparer/RelevantCodes.zip

Reply

Anshoo Arora June 28, 2010 at 10:32 am

Manpreet, please see this post: http://relevantcodes.com/vbscript-compare-2-excel-files/

The DLL is mentioned towards the end under the following title: Update: RelevantCodes.Comparer COM DLL

Reply

Anshoo Arora June 22, 2010 at 12:26 pm

I haven’t tried a lot of such implementations outside of QTP as it would be an overhead in real-time projects, but I can see that it may be possible at least partially. Tarun has had some success with this and he may be able to better advise you with this.

I generally use custom DLLs for coding APIs that are outside of QTP’s control and VBScript does very slow. One such DLL can be found here: http://relevantcodes.com/Articles/RelevantCodes.Comparer/RelevantCodes.zip

Reply

Anshoo Arora June 22, 2010 at 12:21 pm

Raju,

What kind of exceptions? A few ways that can be used for Error/Exception Handling are:

1. Recovery Scenarios
2. On Error Resume Next
3. Using Conditional statements – but this is only beneficial if you expect an error in a certain position but extremely helpful
4. Use QTP’s Test Settings where we can specify what will happen when an error occurs

Reply

Manpreet June 18, 2010 at 9:13 am

Hi Anshoo,

There is a query.
is it possble to execute the qtp commands outside of qtp through say C# or Vb.net.
if Yes then how.
if No then is there any way we can encrypt the vbscript code.

gone through your site the containt are realy nice.

Thanks in advance

Manpreet

Reply

Raju June 15, 2010 at 11:58 pm

Hi Anoosh,
Could you pls explain how to handle exceptions using both VB script and recovery scenarios with examples.

Reply

Chetan May 5, 2010 at 4:21 pm

Hi Anshoo

Just need your help.Although i have downloaded the assembly and registered it as well. But if you could just give an example(code) for how to compare two images. It would be of great help.

Thanks
Chetan

Reply

Anshoo Arora May 6, 2010 at 1:39 pm

If you want, I can e-mail you the C# code from the assembly, and you can convert it to VBScript later..

Reply

Kevin May 4, 2010 at 2:53 pm

Hi Anshoo,
This looks like exactly what I was searching for! Is this script intended to be able to run as-is after downloading? Do any parts of the script need to be customized before running? Thank you!

Reply

Anshoo Arora May 5, 2010 at 2:37 am

Hi Kevin,

For the VBScript code as well as the .NET DLL, the code can be run as-is off QTP or a .VBS file. However, if you’re using the DLL, you will have to register it using RegAsm utility before using it.

Reply

Mark May 2, 2010 at 5:54 am

great post as usual!

Reply

Anshoo Arora April 26, 2010 at 1:43 am

The following .NET assembly is available as well to download: here.

Snapshot of the assembly can be found here.

To register the assembly to your PC, the following code can be used:

RegAsm C:\RelevantCodes.dll /codebase

or

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe C:\RelevantCodes.dll /codebase

Once done, the code can be used in QTP like this:

Set oCompare = CreateObject("RelevantCodes.Comparer")

oCompare. should give you the proper intellisense to use all methods. The assembly compares the following items:

  1. Arrays
  2. Excel Sheets
  3. Images
  4. Integers
  5. Strings
  6. TextFiles

Reply

Marc April 19, 2010 at 5:15 pm

In your class, you use a dictionary object, fill the Dictionary.Key with the concatenation of row and column separated by “|”, and leave the Dictionary.Item empty. Later you split the Key by the separator “|” in order to process the row and column.

Why do you use a dictionary? Right now you just use the Keys, because you cannot use both Key (to store Row) and Item (to store Column). That would result in a conflict if multiple cells in a single row are a mismatch, right?
In a dictionary, the Key identifies the Item, which is obviously not the case in this compare: row does not identify column. So using a dictionary apparently is not the appropriate choice here. Why not simply use an array?
Of course your code works, but it’s not a solution that I’d use in a tutorial.

By the way: an even simpler and faster way for highlighting mismatches is to use Excel’s conditional formatting.

Reply

Anshoo Arora April 19, 2010 at 9:20 pm

Marc,

Thanks for your valuable feedback.

All your observations are correct. I did consider the array approach before posting this code here. It may look more elegant, and more “correct” in a way but in my testing, this approach continuously gave better performance figures in both Normal and Fast run modes.

By the way: an even simpler and faster way for highlighting mismatches is to use Excel’s conditional formatting.

Can you please share this approach with the community? How will you use Conditional Formatting against 2 sheets in different workbooks through VBScript? I would love to see and share this approach with my readers. Thank you!

Reply

ck April 15, 2010 at 9:50 am

excel 1 : 2 rows and 4 col
excel 2 : 5 rows and 3 col

I want to compare them from both ways – compare excel 1 vs 2 and 2 vs 1 in one function .
if i run your function it gave me an error on the following line :
If arrRangeUno(iCellUno, iCellDos) arrRangeDos(iCellUno, iCellDos) Then
what should I chage so it will compare from both side . a>b and b>a

Reply

Anshoo Arora April 19, 2010 at 9:26 pm

I think this error is caused because the Range object retrieved from the Sheets are of different sizes. This code is created to work with like ranges. I should have added this as a limitation. Thanks ck!

Reply

Christian Deserich April 3, 2010 at 9:37 pm

All I have to say is…

Wow.

What a great class. Thanks for sharing, this should save a lot of people a lot of time.

Reply

Satishkumar Dega March 31, 2010 at 5:00 am

Thanx Anshoo Arora for giving reply.

Reply

Anshoo Arora April 1, 2010 at 7:34 pm

:)

Reply

Satishkumar Dega March 28, 2010 at 11:55 pm

Hi Anshoo,
I am the biginer of the performence testing.I want to learn Load runner.
Like Your providing QTP very good examples and good articles in this way is there any website is there tell me.
If you provide that information that is very use full for me.

Reply

Anshoo Arora March 28, 2010 at 11:58 pm
Satishkumar Dega March 24, 2010 at 6:40 am

Hi Anshoo Arora,
Have you any idea on loadrunner or can you suggest someone as like you who can help me out regarding this.

Reply

Anshoo Arora March 24, 2010 at 10:33 am

Hi Satish,

What topics do you need assistance with? There are some excellent websites teaching LR and Performance Testing concepts..

Reply

Anonymous May 10, 2010 at 1:43 pm

Thanks,
I got the solution i needed.

But in btw can you help me with this:-

I have created a reusable action with 6 input parameters. After that in another test script i have called to that reusable action using ‘Call to existing action’.
The code for the same is like this:
RunAction “Action1 [CPO_WORKFLOW]“,oneIteration,
param1,param2,param3,param4,param5,param6

But when i run the script it is giving following error:
You specified more parameters in your RunAction statement than the number of parameters defined for the action.

Line (26): “RunAction “Action1 [CPO_WORKFLOW]“, oneIteration, version_of_app, version, workflowtype, workflowname, workflowjobrun, workflowname_frame”.

Can you tell me what is wrong with this code…

Reply

Vijju October 22, 2010 at 8:03 pm

I run this on QTP 9.2

Reply

Anshoo Arora November 14, 2010 at 5:19 pm

Vijju,

This may be because the ranges of both files are not equal.. this formula is quite basic and used only for like ranges.

Reply

{ 2 trackbacks }

Previous post:

Next post: