Function Find_Possible(DataList, ByVal TargetValue As Double, _
ByVal elements As Integer, ByVal numselect As Integer)
Dim SolutionArray, Counter As Double, cnt As Double
Dim SumCounter As Integer, SubtotalSum1 As Double, SubtotalSum2 As Double
Dim SubtotalSum3 As Double, SubtotalSum4 As Double, SubtotalSum5 As Double
Dim SubtotalSum6 As Double, SubtotalSum7 As Double, SubtotalSum8 As Double
Dim SubtotalSum9 As Double, SubtotalSum10 As Double, SubtotalSum11 As Double
Dim SubtotalSum12 As Double, SubtotalSum13 As Double, SubtotalSum14 As Double
Dim SubtotalSum15 As Double, SubtotalSum16 As Double, SubtotalSum17 As Double
Dim SubtotalSum18 As Double, SubtotalSum19 As Double, SubtotalSum20 As Double
Dim SubtotalSum21 As Double
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer, f As Integer
Dim g As Integer, h As Integer, i As Integer, j As Integer, k As Integer, l As Integer
Dim m As Integer, n As Integer, o As Integer, p As Integer, q As Integer, r As Integer
Dim s As Integer, t As Integer, u As Integer, v As Integer, w As Integer, x As Integer
Set fn = Application.WorksheetFunction
If numselect<= 6 Then ReDim SolutionArray(1 To fn.Combin(elements, numselect)) As String
Select Case numselect
Case 0
Counter = Counter + 1
SolutionArray(1) = 0
Case elements
If fn.Sum(DataList) = TargetValue Then
Counter = Counter + 1
SolutionArray(1) = DataList
End If
Case Is > elements
Counter = Counter + 1
SolutionArray(1) = 0
Case 1
For a = 1 To elements
If fn.Round(DataList(a), 2) = TargetValue Then
Counter = Counter + 1
SolutionArray(Counter) = DataList(a)
End If
Next a
Case 2
For a = 1 To elements + 1 - numselect
For b = a + 1 To elements
SubtotalSum1 = fn.Round(DataList(a) + DataList(b), 2)
If SubtotalSum1 = TargetValue Then
Counter = Counter + 1
SolutionArray(Counter) = DataList(a) & " | " & DataList(b)
End If
Next b: Next a
Case 3
For a = 1 To elements + 1 - numselect
For b = a + 1 To elements + 2 - numselect
For c = b + 1 To elements
SubtotalSum1 = fn.Round(DataList(a) + DataList(b), 2)
SubtotalSum2 = fn.Round(SubtotalSum1 + DataList(c), 2)
If SubtotalSum1 > TargetValue Then GoTo Exit3_1
If SubtotalSum2 > TargetValue Then
GoTo Exit3
ElseIf SubtotalSum2 = TargetValue Then
Counter = Counter + 1
SolutionArray(Counter) = DataList(a) & " | " & DataList(b) _
& " | " & DataList(c)
End If
Next c
Exit3:
Next b
Exit3_1:
Next a
Case 4
For a = 1 To elements + 1 - numselect
For b = a + 1 To elements + 2 - numselect
For c = b + 1 To elements + 3 - numselect
For d = c + 1 To elements
SubtotalSum1 = fn.Round(DataList(a) + DataList(b), 2)
SubtotalSum2 = fn.Round(SubtotalSum1 + DataList(c), 2)
SubtotalSum3 = fn.Round(SubtotalSum2 + DataList(d), 2)
If SubtotalSum1 > TargetValue Then GoTo Exit4_2
If SubtotalSum2 > TargetValue Then GoTo Exit4_1
If SubtotalSum3 > TargetValue Then
GoTo Exit4
ElseIf SubtotalSum3 = TargetValue Then
Counter = Counter + 1
SolutionArray(Counter) = DataList(a) & " | " & DataList(b) _
& " | " & DataList(c) & " | " & DataList(d)
End If
Next d
Exit4:
Next c
Exit4_1:
Next b
Exit4_2:
Next a
Case 5
For a = 1 To elements + 1 - numselect
For b = a + 1 To elements + 2 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b), HoldingArray(2), 0)) Then GoTo Exit5_3
For c = b + 1 To elements + 3 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c), HoldingArray(3), 0)) Then GoTo Exit5_2
For d = c + 1 To elements + 4 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d), _
HoldingArray(4), 0)) Then GoTo Exit5_1
For e = d + 1 To elements
SubtotalSum1 = fn.Round(DataList(a) + DataList(b), 2)
SubtotalSum2 = fn.Round(SubtotalSum1 + DataList(c), 2)
SubtotalSum3 = fn.Round(SubtotalSum2 + DataList(d), 2)
SubtotalSum4 = fn.Round(SubtotalSum3 + DataList(e), 2)
If SubtotalSum1 > TargetValue Then GoTo Exit5_3
If SubtotalSum2 > TargetValue Then GoTo Exit5_2
If SubtotalSum3 > TargetValue Then GoTo Exit5_1
If SubtotalSum4 > TargetValue Then
GoTo Exit5
ElseIf SubtotalSum4 = TargetValue Then
Counter = Counter + 1
SolutionArray(Counter) = DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) & " | " & DataList(e)
End If
Next e
Exit5:
Next d
Exit5_1:
Next c
Exit5_2:
Next b
Exit5_3:
Next a
Case 6
For a = 1 To elements + 1 - numselect
For b = a + 1 To elements + 2 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b), HoldingArray(2), 0)) Then GoTo Exit6_4
For c = b + 1 To elements + 3 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c), HoldingArray(3), 0)) Then GoTo Exit6_3
For d = c + 1 To elements + 4 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d), _
HoldingArray(4), 0)) Then GoTo Exit6_2
For e = d + 1 To elements + 5 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) _
& " | " & DataList(e), HoldingArray(5), 0)) Then GoTo Exit6_1
For f = e + 1 To elements
SubtotalSum1 = fn.Round(DataList(a) + DataList(b), 2)
SubtotalSum2 = fn.Round(SubtotalSum1 + DataList(c), 2)
SubtotalSum3 = fn.Round(SubtotalSum2 + DataList(d), 2)
SubtotalSum4 = fn.Round(SubtotalSum3 + DataList(e), 2)
SubtotalSum5 = fn.Round(SubtotalSum4 + DataList(f), 2)
If SubtotalSum1 > TargetValue Then GoTo Exit6_4
If SubtotalSum2 > TargetValue Then GoTo Exit6_3
If SubtotalSum3 > TargetValue Then GoTo Exit6_2
If SubtotalSum4 > TargetValue Then GoTo Exit6_1
If SubtotalSum5 > TargetValue Then
GoTo Exit6
ElseIf SubtotalSum5 = TargetValue Then
Counter = Counter + 1
SolutionArray(Counter) = DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) & " | " & DataList(e) & " | " & DataList(f)
End If
Next f
Exit6:
Next e
Exit6_1:
Next d
Exit6_2:
Next c
Exit6_3:
Next b
Exit6_4:
Next a
Case 7
For a = 1 To elements + 1 - numselect
For b = a + 1 To elements + 2 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b), HoldingArray(2), 0)) Then GoTo Exit7_5
For c = b + 1 To elements + 3 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c), HoldingArray(3), 0)) Then GoTo Exit7_4
For d = c + 1 To elements + 4 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d), _
HoldingArray(4), 0)) Then GoTo Exit7_3
For e = d + 1 To elements + 5 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) _
& " | " & DataList(e), HoldingArray(5), 0)) Then GoTo Exit7_2
For f = e + 1 To elements + 6 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) _
& " | " & DataList(e) & " | " & DataList(f), HoldingArray(6), 0)) Then GoTo Exit7_1
For g = f + 1 To elements
Application.StatusBar = a & "|" & b & "|" & c & "|" & d & "|" & e & "|" & f
SubtotalSum1 = fn.Round(DataList(a) + DataList(b), 2)
SubtotalSum2 = fn.Round(SubtotalSum1 + DataList(c), 2)
SubtotalSum3 = fn.Round(SubtotalSum2 + DataList(d), 2)
SubtotalSum4 = fn.Round(SubtotalSum3 + DataList(e), 2)
SubtotalSum5 = fn.Round(SubtotalSum4 + DataList(f), 2)
SubtotalSum6 = fn.Round(SubtotalSum5 + DataList(g), 2)
If SubtotalSum1 > TargetValue Then GoTo Exit7_5
If SubtotalSum2 > TargetValue Then GoTo Exit7_4
If SubtotalSum3 > TargetValue Then GoTo Exit7_3
If SubtotalSum4 > TargetValue Then GoTo Exit7_2
If SubtotalSum5 > TargetValue Then GoTo Exit7_1
If SubtotalSum6 > TargetValue Then
GoTo Exit7
ElseIf SubtotalSum6 = TargetValue Then
Counter = Counter + 1
If Counter = 1 Then
ReDim SolutionArray(1 To Counter) As String
Else
ReDim Preserve SolutionArray(1 To Counter) As String
End If
SolutionArray(Counter) = DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) & " | " & DataList(e) _
& " | " & DataList(f) & " | " & DataList(g)
End If
Next g
Exit7:
Next f
Exit7_1:
Next e
Exit7_2:
Next d
Exit7_3:
Next c
Exit7_4:
Next b
Exit7_5:
Next a
Case 8
For a = 1 To elements + 1 - numselect
For b = a + 1 To elements + 2 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b), HoldingArray(2), 0)) Then GoTo Exit8_6
For c = b + 1 To elements + 3 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c), HoldingArray(3), 0)) Then GoTo Exit8_5
For d = c + 1 To elements + 4 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d), _
HoldingArray(4), 0)) Then GoTo Exit8_4
For e = d + 1 To elements + 5 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) _
& " | " & DataList(e), HoldingArray(5), 0)) Then GoTo Exit8_3
For f = e + 1 To elements + 6 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) _
& " | " & DataList(e) & " | " & DataList(f), HoldingArray(6), 0)) Then GoTo Exit8_2
For g = f + 1 To elements + 7 - numselect
If Not IsError(Application.Match(DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) _
& " | " & DataList(e) & " | " & DataList(f) & " | " & DataList(g), HoldingArray(7), 0)) Then GoTo Exit8_1
For h = g + 1 To elements
Application.StatusBar = a & "|" & b & "|" & c & "|" & d & "|" & e & "|" & f
SubtotalSum1 = fn.Round(DataList(a) + DataList(b), 2)
SubtotalSum2 = fn.Round(SubtotalSum1 + DataList(c), 2)
SubtotalSum3 = fn.Round(SubtotalSum2 + DataList(d), 2)
SubtotalSum4 = fn.Round(SubtotalSum3 + DataList(e), 2)
SubtotalSum5 = fn.Round(SubtotalSum4 + DataList(f), 2)
SubtotalSum6 = fn.Round(SubtotalSum5 + DataList(g), 2)
SubtotalSum7 = fn.Round(SubtotalSum6 + DataList(h), 2)
If SubtotalSum1 > TargetValue Then GoTo Exit8_6
If SubtotalSum2 > TargetValue Then GoTo Exit8_5
If SubtotalSum3 > TargetValue Then GoTo Exit8_4
If SubtotalSum4 > TargetValue Then GoTo Exit8_3
If SubtotalSum5 > TargetValue Then GoTo Exit8_2
If SubtotalSum6 > TargetValue Then GoTo Exit8_1
If SubtotalSum7 > TargetValue Then
GoTo Exit8
ElseIf SubtotalSum7 = TargetValue Then
Counter = Counter + 1
If Counter = 1 Then
ReDim SolutionArray(1 To Counter) As String
Else
ReDim Preserve SolutionArray(1 To Counter) As String
End If
SolutionArray(Counter) = DataList(a) & " | " & DataList(b) & " | " & DataList(c) & " | " & DataList(d) & " | " & DataList(e) _
& " | " & DataList(f) & " | " & DataList(g) & " | " & DataList(h)
End If
Next h
Exit8:
Next g
Exit8_1:
Next f
Exit8_2:
Next e
Exit8_3:
Next d
Exit8_4:
Next c
Exit8_5:
Next b
Exit8_6:
Next a
----snip lots of similar code---
End Select
If Counter = 0 Then
ReDim SolutionArray(1 To 1)
SolutionArray(1) = "No solutions"
Else
ReDim Preserve SolutionArray(1 To Counter) As String
End If
Find_Possible = SolutionArray
Erase SolutionArray
End Function