View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
Dana DeLouis Dana DeLouis is offline
external usenet poster
 
Posts: 947
Default compare numbers -- recursive?

Just to be different...

Function AnyXEqual(n, ParamArray v() As Variant) As Boolean
AnyXEqual = (UBound(Filter(v, WorksheetFunction.Mode(v), True)) + 1) =
n
End Function

Sub testit()
Debug.Print AnyXEqual(3, 67, 50, 67, 98)
Debug.Print AnyXEqual(2, 67, 50, 67, 98)
Debug.Print AnyXEqual(4, 67, 50, 67, 67)
Debug.Print AnyXEqual(3, 67, 50, 67, 67)
Debug.Print AnyXEqual(2, 67, 50, 67, 67)
End Sub

--
HTH. :)
Dana DeLouis
Windows XP, Office 2003


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.