0

I have nice code to get sum of subsets in Python:

# Python3 program to print sums of
# all possible subsets.

# Prints sums of all subsets of arr[l..r]
def subsetSums(arr, l, r, sum = 0):

    # Print current subset
    if l > r:
        print (sum, end = " ")
        return

    # Subset including arr[l]
    subsetSums(arr, l + 1, r, sum + arr[l])

    # Subset excluding arr[l]
    subsetSums(arr, l + 1, r, sum)

# Driver code
arr = [5.76, 4.45, 3.2]
n = len(arr)
subsetSums(arr, 0, n - 1)

How to do same thing in Excel VBA?

1
  • Where is name 'arr' defined btw? Commented May 7, 2018 at 8:51

1 Answer 1

1

Try this code:

Function subsetSums(arr As Variant, l As Long, r As Long, sum As Long)
    If l > r Then
        Debug.Print (sum)
        Exit Function
    End If
    'Subset including arr[l]
    a = subsetSums(arr, l + 1, r, sum + arr(l))

    'Subset excluding arr[l]
    a = subsetSums(arr, l + 1, r, sum)
End Function

Sub Driver()
    Dim arr As Variant
    arr = Array(1, 3, 5)
    a = subsetSums(arr, LBound(arr), UBound(arr), 0)
End Sub

In a code, a variable is used just to satisfy VBA syntax, when you call a function, its result must be assigned to a variable.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't need this dummy var a. Either use call or omit the parentheses (like you do for a sub)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.