I have an Excel 2007 spreadsheet populated with four columns (V1 V2, V3 V4) that contain values (integers [0-20]) separated by a comma. Values need not be continuous.
If each value can only appear once in each cell:
Function IfAtLeast(rng As Range, num As Integer)
Const SEP As String = ","
Dim c As Range, d As Object, arr, i As Long, tmp, k
Dim rv As String
rv = ""
Set d = CreateObject("scripting.dictionary")
For Each c In rng
arr = Split(c.Value, SEP)
For i = LBound(arr) To UBound(arr)
tmp = Trim(arr(i))
If Not d.exists(tmp) Then d.Add tmp, 0
d(tmp) = d(tmp) + 1
Next i
Next c
For Each k In d.keys
If d(k) >= num Then rv = rv & IIf(Len(rv) > 0, ",", "") & k
Next k
IfAtLeast = rv
End Function
Usage:
=IfAtLeast(A2:D2,2)
EDIT: if you're having problems trouble-shooting a UDF then it is often easier to debug the issue using a sub
Sub Tester()
debug.print IfAtLeast(Activesheet.Range("A2:D2"), 2)
End Sub