View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
RB Smissaert RB Smissaert is offline
external usenet poster
 
Posts: 2,452
Default compare numbers -- recursive?

If the number of numbers to compare can be any I would do it like this:

Function AnyXEqual2(X, arr As Variant) As Boolean

Dim i As Long
Dim coll As Collection

Set coll = New Collection

On Error Resume Next
For i = LBound(arr) To UBound(arr)
coll.Add vbNull, CStr(arr(i))
Next

If (UBound(arr) + (2 - LBound(arr))) - coll.Count = X Then
AnyXEqual2 = True
End If

End Function

arr is an 0-based or 1-based 1-D array of the numbers to check.


RBS

wrote in message
ups.com...
I'm trying to create a function that will tell me if X numbers out of 4
are equal. Something like:

Function AnyXEqual(x As Integer, int1 As Integer, int2 As Integer, int3
As Integer, int4 As Integer) As Boolean
'[code]
End Function

So

AnyXEqual(3, 67, 50, 67, 98) = False
AnyXEqual(2, 67, 50, 67, 98) = True

AnyXEqual(4, 67, 50, 67, 67) = False
AnyXEqual(3, 67, 50, 67, 67) = True
AnyXEqual(2, 67, 50, 67, 67) = True

I thought about using a For i = 1 to x loop (or 2) to compare them, but
I think that would only work if x was 2 ... if x was 3 I would need a
nested loop, and if x was 4 I would need another nested loop.

Something tells me this is a perfect situation for a recursive
function, but my brain has trouble thinking on that level. Any ideas?
Thanks.