LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
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




 
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
copy of excel file not showing formulal/function in the function b oaallam Excel Discussion (Misc queries) 4 September 6th 07 01:20 PM
Excel 2002: Auto Sum function not working in large Excel file Mr. Low Excel Discussion (Misc queries) 3 May 25th 07 03:36 PM
challenge! javascript function into excel function Kamila Excel Worksheet Functions 2 February 19th 07 06:35 AM
Excel Workday Function with another function Monique Excel Discussion (Misc queries) 2 April 27th 06 01:11 PM
Can you nest a MID function within a IF function in Excel Dawn-Anne Excel Worksheet Functions 2 March 4th 05 01:37 PM


All times are GMT +1. The time now is 09:31 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"