Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
#38304
Olá!
Procurei muito e, por não encontrar nenhum modelo aplicável, recorri ao fórum como última esperança.
O problema é semelhante à "mochila booleana".

Tenho uma coluna A com valores variados e repetidos.
Preciso encontrar a soma ideal destes valores, que se aproximem de um valor pré-definido X.
Ou seja, um código para efetuar todas as combinações de soma e me apontar as somas que mais se aproximam do valor X.

O mais próximo que encontrei na internet, foi o código abaixo. Contudo, ao chamar a função, só obtenho erro.
Solicito ajuda dos experts, porque o cérebro já fritou tentando resolver essa parada. :shock: :roll:

OBS.: Códigos em C, C#, C++ e Java não são soluções possíveis pois os dados vêm sempre em tabelas excel.

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
'recursividade:
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
'incrementando o array
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 i = -1 And UBound(arrIndices) < UBound(arr) Then
ReDim arrIndices(UBound(arrIndices) + 1)
For i = 0 To UBound(arrIndices)
arrIndices(i) = i
Next
ElseIf i = -1 And UBound(arrIndices) = UBound(arr) Then
fSubSet = False
Exit Function
End If
fSubSet = fSubSet(arr, goal, arrIndices)
End Function
Você não está autorizado a ver ou baixar esse anexo.
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord