Home |
Search |
Today's Posts |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Recursive Functions...maybe | Excel Worksheet Functions | |||
compare 2 columns of numbers and place the matched numbers in a 3r | Excel Discussion (Misc queries) | |||
Recursive Functio help | Excel Worksheet Functions | |||
VLOOKUP should compare numbers stored as text to plain numbers. | Excel Worksheet Functions | |||
recursive sums | Excel Worksheet Functions |