LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default compare numbers -- recursive?

you've seen solutions using the collection. Its even easier using the
scripting Dictioanry object. This, unlike a collection, allows one to test if
a key already exists or not.

In the example below, I add each of the pass parameters to the dictionary.
If it doesn't already exist as a key, I add it, setting th evalue to 1. If
the value is already in the dictionary's key, I increment the value by 1,
then test if it matches the 'x' value.

In the IDE set a reference to the Microsoft Scripting Runtime DLL the add
the function below...

Option Explicit
Function AnyXEqual(count As Long, values As Range) As Boolean
Dim index As Long
Dim val As Variant
Dim sVal As String
Dim dic As Scripting.Dictionary
Set dic = New Scripting.Dictionary
For Each val In values
sVal = CStr(val)
If dic.Exists(sVal) Then
dic.Item(sVal) = dic.Item(sVal) + 1
If dic.Item(sVal) = count Then
AnyXEqual = True
Exit Function
End If
Else
dic.Add sVal, 1
End If
Next
Set dic = Nothing
End Function

Note: one any value set the function value to TRUE, we don't need to test
any more, so the code exits the function
The default for a boolean is FALSE...some purists might say it should be
explicitly set, but not me, thats what defaults are for. But it does make
debugging easier. Add it if you want
AnyXEqual = FALSE
just before the End Function OR just after the DIM statements at the start.

"Helmut Weber" wrote:

Public Function AnyXEqual(x As Integer, int1 As Integer, _
int2 As Integer, int3 As Integer, int4 As Integer) As Boolean
Dim arr(1 To 4) As Long
Dim j As Long
Dim l As Long
Dim m As Long
Dim c As Long

AnyXEqual = False
arr(1) = int1
arr(2) = int2
arr(3) = int3
arr(4) = int4
' sort it
For j = 1 To 4
For m = 1 To 4
If arr(j) < arr(m) Then
l = arr(j)
arr(j) = arr(m)
arr(m) = l
End If
Next
Next
c = 1
For l = 1 To 3
If arr(l) = arr(l + 1) Then
c = c + 1
End If
Next
If c = x Then AnyXEqual = True
End Function

Sub test000987()
MsgBox AnyXEqual(3, 7, 2, 2, 2)
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive Functions...maybe busboy10 Excel Worksheet Functions 0 March 28th 11 09:46 PM
compare 2 columns of numbers and place the matched numbers in a 3r Clive[_2_] Excel Discussion (Misc queries) 5 November 8th 09 12:50 AM
Recursive Functio help BigBobbo Excel Worksheet Functions 1 May 10th 06 07:23 PM
VLOOKUP should compare numbers stored as text to plain numbers. VLOOKUP - Numbers stored as text Excel Worksheet Functions 0 March 31st 06 05:53 PM
recursive sums Joe Excel Worksheet Functions 6 July 17th 05 09:45 AM


All times are GMT +1. The time now is 04:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"