Same Contents, Regardless of Order
Oops that does not quite work... Try this one...
Sub Test()
Dim ary1 As Variant
Dim ary2 As Variant
Dim ary3 As Variant
Dim ary4 As Variant
ary1 = Array(1, 2, 3, 4, 5, 6)
ary2 = Array(1, 6, 4, 3, 5, 2)
ary3 = Array(1, 1, 4, 3, 5, 2)
ary4 = Array(1, 2, 3, 4, 5, 6, 7)
MsgBox ArrayCompare(ary1, ary2)
MsgBox ArrayCompare(ary1, ary3)
MsgBox ArrayCompare(ary3, ary1)
MsgBox ArrayCompare(ary2, ary4)
End Sub
Public Function ArrayCompare(ByRef ary1 As Variant, ByRef ary2 As Variant)
As Boolean
Dim lngAry1 As Long
Dim lngAry2 As Long
Dim blnFound As Boolean
ArrayCompare = True
If UBound(ary1) < UBound(ary2) Then
ArrayCompare = False
Exit Function
End If
blnFound = False
For lngAry1 = LBound(ary1) To UBound(ary1)
For lngAry2 = LBound(ary2) To UBound(ary2)
If ary1(lngAry1) = ary2(lngAry2) Then
blnFound = True
Exit For
End If
Next lngAry2
If blnFound = False Then
ArrayCompare = False
Exit Function
End If
blnFound = False
Next lngAry1
For lngAry2 = LBound(ary2) To UBound(ary2)
For lngAry1 = LBound(ary1) To UBound(ary1)
If ary1(lngAry1) = ary2(lngAry2) Then
blnFound = True
Exit For
End If
Next lngAry1
If blnFound = False Then
ArrayCompare = False
Exit Function
End If
blnFound = False
Next lngAry2
End Function
--
HTH...
Jim Thomlinson
"Jim Thomlinson" wrote:
This should do it. The only way that I know of is a brute force attack.
Sub Test()
Dim ary1 As Variant
Dim ary2 As Variant
Dim ary3 As Variant
Dim ary4 As Variant
ary1 = Array(1, 2, 3, 4, 5, 6)
ary2 = Array(1, 6, 4, 3, 5, 2)
ary3 = Array(1, 1, 4, 3, 5, 2)
ary4 = Array(1, 2, 3, 4, 5, 6, 7)
MsgBox ArrayCompare(ary1, ary2)
MsgBox ArrayCompare(ary1, ary3)
MsgBox ArrayCompare(ary2, ary4)
End Sub
Public Function ArrayCompare(ByRef ary1 As Variant, ByRef ary2 As Variant)
As Boolean
Dim lngAry1 As Long
Dim lngAry2 As Long
Dim blnFound As Boolean
ArrayCompare = True
If UBound(ary1) < UBound(ary2) Then
ArrayCompare = False
Exit Function
End If
blnFound = False
For lngAry1 = LBound(ary1) To UBound(ary1)
For lngAry2 = LBound(ary2) To UBound(ary2)
If ary1(lngAry1) = ary2(lngAry2) Then
blnFound = True
Exit For
End If
Next lngAry2
If blnFound = False Then
ArrayCompare = False
Exit For
End If
blnFound = False
Next lngAry1
End Function
--
HTH...
Jim Thomlinson
"Gary''s Student" wrote:
I need a Boolean function that, given two input arrays of equal size, will
return TRUE if the contents of the arrays are the same (apart from order),
otherwise FALSE.
For example, if array 1 contained:
1,2,3,4
and array 2 contained:
2,3,1,4
then the function should return TRUE
If array 1 contained:
1,1,6,7
and array 2 contained:
7,1,6,2
then the function should return FALSE.
--
Gary''s Student - gsnu2007xx
|