Thread: Excel Function
View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
LINDA LINDA is offline
external usenet poster
 
Posts: 205
Default Excel Function


--
Linda


"ilia" wrote:

Here's one I picked off from one of these groups for my Personal
Macros Workbook recently. It works pretty well.

After you paste this into a standard module, ensure that Tools-
References has these two checked:


'Microsoft Scripting Runtime
'Microsoft VBScript Regular Expressions 1.0

Hope this helps.



Sub FindSums()
'This *REQUIRES* VBAProject references to
'Microsoft Scripting Runtime
'Microsoft VBScript Regular Expressions 1.0
'Written by Harlan Grove


' Const tol As Double = 0.000001 'modify as needed
Dim c As Variant
Dim tol As Double, Temp As Variant


Dim j As Long, K As Long, N As Long, p As Boolean
Dim s As String, t As Double, u As Double
Dim V As Variant, x As Variant, y As Variant
Dim dc1 As New Dictionary, dc2 As New Dictionary
Dim dcn As Dictionary, dco As Dictionary
Dim re As New RegExp


re.Global = True
re.IgnoreCase = True


On Error Resume Next


Set x = Application.InputBox( _
Prompt:="Enter range of values:", _
Title:="findsums", _
Default:="", _
Type:=8 _
)


If x Is Nothing Then
Err.Clear
Exit Sub
End If


y = Application.InputBox( _
Prompt:="Enter target value:", _
Title:="findsums", _
Default:="", _
Type:=1 _
)

If VarType(y) = vbBoolean Then
Exit Sub
Else
t = y
End If

Temp = Application.InputBox( _
Prompt:="Enter tolerance value:", _
Title:="findsums", _
Default:="", _
Type:=1 _
)

If VarType(Temp) = vbBoolean Then
tol = 0.01
Else
tol = Temp
End If


On Error GoTo 0


Set dco = dc1
Set dcn = dc2


Call recsoln


For Each y In x.Value2
If VarType(y) = vbDouble Then
If Abs(t - y) < tol Then
recsoln "+" & Format(y)


ElseIf dco.Exists(y) Then
dco(y) = dco(y) + 1


ElseIf y < t - tol Then
dco.Add Key:=y, Item:=1


c = CDec(c + 1)
If (c Mod 100 = 0) Then
Application.StatusBar = "[1] " & Format(c)
End If


End If


End If
Next y


N = dco.Count


ReDim V(1 To N, 1 To 3)


For K = 1 To N
V(K, 1) = dco.Keys(K - 1)
V(K, 2) = dco.Items(K - 1)
Next K


qsortd V, 1, N


For K = N To 1 Step -1
V(K, 3) = V(K, 1) * V(K, 2) + V(IIf(K = N, N, K + 1), 3)
If V(K, 3) t Then dcn.Add Key:="+" & Format(V(K, 1)), Item:=V(K,
1)
Next K


On Error GoTo CleanUp
Application.EnableEvents = False
Application.Calculation = xlCalculationManual


For K = 2 To N
dco.RemoveAll
swapo dco, dcn


For Each y In dco.Keys
p = False


For j = 1 To N
If V(j, 3) < t - dco(y) - tol Then Exit For


x = V(j, 1)
s = "+" & Format(x)
If Right(y, Len(s)) = s Then p = True


If p Then
re.Pattern = "\" & s & "(?=(\+|$))"
If re.Execute(y).Count < V(j, 2) Then
u = dco(y) + x


If Abs(t - u) < tol Then
recsoln y & s


ElseIf u < t - tol Then
dcn.Add Key:=y & s, Item:=u


c = CDec(c + 1)
Application.StatusBar = "[" & Format(K) & "] " &
Format(c)


End If
End If
End If
Next j
Next y


If dcn.Count = 0 Then Exit For
Next K


If (recsoln() = 0) Then _
MsgBox Prompt:="all combinations exhausted", Title:="No Solution"


CleanUp:
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.StatusBar = False


End Sub


Private Function recsoln(Optional s As String)
Const OUTPUTWSN As String = "findsums solutions" 'modify to taste


Static r As Range
Dim ws As Worksheet


If s = "" And r Is Nothing Then
On Error Resume Next
Set ws = ActiveWorkbook.Worksheets(OUTPUTWSN)


If ws Is Nothing Then
Err.Clear
Application.ScreenUpdating = False
Set ws = ActiveSheet
Set r = Worksheets.Add.Range("A1")
r.Parent.Name = OUTPUTWSN
ws.Activate
Application.ScreenUpdating = False


Else
ws.Cells.Clear
Set r = ws.Range("A1")


End If


recsoln = 0


ElseIf s = "" Then
recsoln = r.Row - 1
Set r = Nothing


Else
r.Value = s
Set r = r.Offset(1, 0)
recsoln = r.Row - 1


End If


End Function


Private Sub qsortd(V As Variant, lft As Long, rgt As Long)
'ad hoc quicksort subroutine
'translated from Aho, Weinberger & Kernighan,
'"The Awk Programming Language", page 161


Dim j As Long, pvt As Long


If (lft = rgt) Then Exit Sub


swap2 V, lft, lft + Int((rgt - lft + 1) * Rnd)


pvt = lft


For j = lft + 1 To rgt
If V(j, 1) V(lft, 1) Then
pvt = pvt + 1
swap2 V, pvt, j
End If
Next j


swap2 V, lft, pvt


qsortd V, lft, pvt - 1
qsortd V, pvt + 1, rgt
End Sub


Private Sub swap2(V As Variant, i As Long, j As Long)
'modified version of the swap procedure from
'translated from Aho, Weinberger & Kernighan,
'"The Awk Programming Language", page 161


Dim t As Variant, K As Long