1. ## Compre two arrays

Hi i am having two arrays "A" and "B". "A" is from Master form which contains active seller list. Another array "B" contains the the sellers list collected from Weblist.

Now i want to compare two arrays and confirm whether all elements from array "A" also available in array "B". Here i used below code and confirm. But i am struggling in one point to produce the fail results. So please give me the logic.

My code

For i = 0 to Ubound(A)-1
For j = 0 to Ubound(B)-1
If A(i)&lt;&gt;"" and A(i) = B(j) then
Print "Pass"
Else
Print "Fail"
End If
Next
Next

But this code produces Fail result even in Pass scenarios.
So please give me some solution.

Thanks.

2. ## Re: Compre two arrays

Here you go:

Public function CompareArrays( aArray1, aArray2 )
bReturn = true
If UBound( aArray1) = UBound( aArray2) Then
For i = 1 to UBound( aArray1 )
If aArray1(i) &lt;&gt; aArray2(i) Then
bReturn = false
Exit for
End If
Next
else
bReturn = false
End If
CompareArrays = bReturn
End Function

3. ## Re: Compre two arrays

Are you certain both arrays contain the same number of elements in the same order?

Your use of Ubound(array) - 1 is going to always skip the last array element. Ubound will return the highest array index and thus accounts for the fact that array indexes start at 0.

4. ## Re: Compre two arrays

This would definitely fail as your programming logic is wrong. You are comparing one element of an array with all other elements and then you are deciding the pass and fail during the inner comparison only.

Look in the below post and i guess you would find some code to compare 2 arrays

4 Donate user defined functions
4.A http://www.sqaforums.com/showflat.php?Number=347046

5. ## Re: Compre two arrays

''this function will compare both arrays returns the difference from second to a text file... you can ognore if you want a difference text file

Const FOR_WRITING = 2

Dim objFso
Dim objOutputFile
Dim strOutputFile

Dim arrFirst
Dim arrSecond
Dim strElementFirst
Dim strElementSecond
Dim blnExistsInSecond

strOutputFile = "C:\test\Differences.txt"

Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(strOutputFile) Then
Set objOutputFile = objFso.OpenTextFile(strOutputFile,FOR_WRITING)
Else
Set objOutputFile = objFso.CreateTextFile(strOutputFile)
End If

arrFirst = Array("A","B","C","D","E")
arrSecond = Array("F","G","H","A","B")

For Each strElementFirst In arrFirst
blnExistsInSecond = False
For Each strElementSecond In arrSecond
If strElementFirst = strElementSecond Then
blnExistsInSecond = True
Exit For
End If
Next
If Not blnExistsInSecond Then
objOutputFile.WriteLine strElementFirst &amp; " does not exist in the second array"
End If
Next
objOutputFile.Close()

Set objOutputFile = Nothing
Set objFso = Nothing&lt;hr noshade size="1"&gt;

6. ## Re: Compre two arrays

Cheers Tarun Lalwani for your efforts appreciated by HP.

7. ## Re: Compre two arrays

Here is a optimal solution for the same now

http://knowledgeinbox.com/articles/v...g-differences/

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.