1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| Private wSh As Excel.Worksheet, MaBalise As Excel.Range
Private iLigne As Long
Private Sub subIter(ByRef Ind() As Long, ByVal iRang As Long, ByRef Source() As Long, _
ByRef Coef() As Long, ByVal iSomme As Long, ByVal iCible As Long)
Dim iTot As Long, i As Integer
For Ind(iRang) = 0 To Coef(iRang)
iTot = iSomme + Ind(iRang) * Source(iRang)
If iRang < UBound(Ind) Then
If iTot < iCible Then Call subIter(Ind, iRang + 1, Source, Coef, iTot, iCible)
Else
If iTot = iCible Then
'on a une solution
For i = 1 To UBound(Ind)
MaBalise.Offset(iLigne, i - 1).Value = Ind(i)
Next i
iLigne = iLigne + 1
End If
End If
Next Ind(iRang)
End Sub
Public Sub subCalculer()
Dim Ind() As Long, Source() As Long, Coef() As Long
Dim Cible As Long, NbSources As Long
Dim i As Integer
'instanciations
Set wSh = ThisWorkbook.Worksheets(1)
Set MaBalise = wSh.Range("C3")
'lecture des paramètres
Cible = wSh.Range("B1").Value
NbSources = wSh.Range("B2").Value
ReDim Ind(1 To NbSources)
ReDim Source(1 To NbSources)
ReDim Coef(1 To NbSources)
'charger les valeurs des sources, et des coef max
For i = 1 To NbSources
Source(i) = MaBalise.Offset(0, i - 1)
Coef(i) = Int(Cible / Source(i))
Next i
'caler la première ligne de résultats
iLigne = 1
'effacer les résultats précédents
wSh.Rows("4:" & Application.Rows.Count).ClearContents
Call subIter(Ind(), 1, Source(), Coef(), 0, Cible)
Set wSh = Nothing
Set MaBalise = Nothing
End Sub |
Partager