I'm trying to run a dynamic programming example using VBA that should limit the amount of times the function is recursively called by storing values in a dictionary.

The problem I'm running into is the function is recursively called before the value can be stored in the dictionary with its generated key.

Typically I would use the 'return' keyword after the dictionary key stored the associated value, but that doesn't seem to work in VBA.
Public Function count_sets_dp(arr, total)
Dim mem As Dictionary
Set mem = New Dictionary
count_sets_dp = dp(arr, total, GetArrLength(arr), mem)
Public Function dp(arr, total, i, mem)
'dynamic programming or memoized solution

Dim Key As String
Dim to_return As Integer

Key = CStr(total) & ":" & CStr(i)
If mem.Exists(Key) Then
      dp = mem(Key)
End If
If total = 0 Then
dp = 1


ElseIf total < 0 Then
dp = 0


ElseIf i < 1 Then
dp = 0


ElseIf total < arr(i) Then
to_return = dp(arr, total, i - 1, mem)

Else: to_return = (dp(arr, total - arr(i), i - 1, mem) + dp(arr, total, i - 1, mem))
mem(Key) = to_return
dp = to_return
End If

End Function
Note: The function returns the correct value, but does not work as intended above. It is recursively calling itself without benefitting from the dynamic solution.

Sub mysub()

Dim timetaken As Double

Dim myArray() As Variant
Dim result As Integer

myArray = [{2,4,6,4,10,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}]

result = count_sets_dp(myArray, 16)

MsgBox (result)

End Sub
Thank you for any help.