I needed a similar recursive function. Here is the code.
*add your own error handling
Public Function fSubSet(arr As Variant, goal As Double, Optional arrIndices As Variant) As Boolean
Dim i As Integer
Dim intSumSoFar As Integer
i = 0
If IsMissing(arrIndices) Then
arrIndices = Array(0)
End If
For i = LBound(arrIndices) To UBound(arrIndices)
intSumSoFar = intSumSoFar + arr(arrIndices(i))
Next
If intSumSoFar = goal Then
For i = LBound(arrIndices) To UBound(arrIndices)
Debug.Print arr(arrIndices(i))
Next
fSubSet = True
Exit Function
End If
'now we increment one piece of the array starting from the last one
i = UBound(arrIndices)
Do While i > -1
If arrIndices(i) + (UBound(arrIndices) - i) < UBound(arr) Then
arrIndices(i) = arrIndices(i) + 1
Exit Do
End If
i = i - 1
Loop
'if we are on the first index of the indices array and it is pushed as far as it can go then reset the array and add one to it if that doesn't make it too big
If i = -1 And UBound(arrIndices) < UBound(arr) Then
ReDim arrIndices(UBound(arrIndices) + 1)
For i = 0 To UBound(arrIndices)
arrIndices(i) = i
Next
'we need to end this monster
ElseIf i = -1 And UBound(arrIndices) = UBound(arr) Then
fSubSet = False
Exit Function
End If
fSubSet = fSubSet(arr, goal, arrIndices)
End Function
Public Function fTestSubSet()
Debug.Print fSubSet(Array(1, 2, 5, 6, 11, 10), 35)
End Function