Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 349
- Office Version
- 2003 or older
- Platform
- Windows
I have a UDF to find the Highest Factors of a Number. It is a UDF with array output With most numbers it works fine, for example with input of 24 it outputs 8, 3, NA; with input of 16 it outputs, 4, 4, NA; but with input of 25 it outputs 5, 5, 5, 5, ... in as many cells selected for array output. I have not been able to fix. Any possible solution?
VBA Code:
Public Function Factors( _
ByVal x As Variant) As Variant
Dim i As Long, j As Long, obj As Object, var() As Variant
Set obj = CreateObject("Scripting.Dictionary")
For i = 2# To (x / 2#)
j = x Mod i
If j = 0# Then
obj.Add i, i
End If
Next i
var = obj.Items()
Select Case UBound(var)
Case 0#, 1#
Factors = var
Case Is >= 2#
Factors = Array(var(UBound(var) - 1#), var(LBound(var) + 1#))
End Select
End Function