Combinaison pour une somme cible

bonjour tout le monde

je cherche a trouver la combinaison d'une plage pour trouver la somme d'une valeur cible.

voila ce que j'ai réussi a faire pour le moment , quelqu’un peut-il m'aider svp

Sub alea()

Dim rng As Range
Dim cell As Range
Dim i As Integer

Set rng = Range(Range("A65536").End(xlUp), Range("A1"))

Do

For Each cell In rng

Cells(4, 4) = cell
Cells(4, 5) = cell
Cells(4, 6) = cell

Cells(4, 7) = Cells(4, 4) + Cells(4, 5) + Cells(4, 6)

Next

Loop While Cells(1, 7) <> Cells(4, 7)

End Sub
62alea-macro.xlsm (15.42 Ko)

Salut !

Utilise la balise code pour améliorer la lisibilité de ton post.

For Each cell In rng

Cells(4, 4) = cell
Cells(4, 5) = cell
Cells(4, 6) = cell

Cells(4, 7) = Cells(4, 4) + Cells(4, 5) + Cells(4, 6)

Next

Dans cette macro tu fais la somme de 3 valeurs identiques ... il faut faire 3 boucles comme des poupées russes !

bonjour Steelson

c'est ce que je cherche a faire mais je sais pas comment y prendre

Hello,

Si VBA n'est pas obligatoire, il y'a le solveur pour ça qui peut le faire aussi, il y'a une démo ici :

https://www.excel-pratique.com/fr/telechargements/doc-excel/cours-excel-solveur-no256.php

waard a écrit :

Hello,

Si VBA n'est pas obligatoire, il y'a le solveur pour ça qui peut le faire aussi, il y'a une démo ici :

https://www.excel-pratique.com/fr/telechargements/doc-excel/cours-excel-solveur-no256.php

merci waard

je vais jeter un coup d’œil

Sinon, voici la correction de ta macro :

Sub alea()

Dim i As Integer
Dim j As Integer
Dim k As Integer

For i = 1 To Range("A65536").End(xlUp).Row - 2
    For j = i + 1 To Range("A65536").End(xlUp).Row - 1
        For k = j + 1 To Range("A65536").End(xlUp).Row

            If Cells(i, 1) + Cells(j, 1) + Cells(k, 1) = Cells(1, 7) Then
                Cells(4, 4) = Cells(i, 1)
                Cells(4, 5) = Cells(j, 1)
                Cells(4, 6) = Cells(k, 1)
                Exit For
            End If

        Next k
    Next j
Next i

End Sub

et pour toutes les solutions de 3 valeurs :

Sub alea()

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim sol As Integer

sol = 0
For i = 1 To Range("A65536").End(xlUp).Row - 2
    For j = i + 1 To Range("A65536").End(xlUp).Row - 1
        For k = j + 1 To Range("A65536").End(xlUp).Row

            If Cells(i, 1) + Cells(j, 1) + Cells(k, 1) = Cells(1, 7) Then
                Cells(4 + sol, 4) = Cells(i, 1)
                Cells(4 + sol, 5) = Cells(j, 1)
                Cells(4 + sol, 6) = Cells(k, 1)
                sol = sol + 1
            End If

        Next k
    Next j
Next i

End Sub
Rechercher des sujets similaires à "combinaison somme cible"