Sorry - you have 4 unknown values (cells D3:G3 - which you want to change), but only two constraints (or equations): that the sum of the 4 variables be equal to A3, and that the Sum of the Values divided by the constants in D2:G2 be another specific value (A3/B3).
They will not give a unique solution - in fact, they will give an almost infinite number of solutions - the first two solutions from my brute force solving are, for example:
1 934 2055 10
1 944 2015 40
You can only solve those for 2 of the unknowns as expressions of the other 2 values.
Here is code that does the brute force solution, for just one value of the first cell (D3 = 1):
Sub FirstSolution()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim i1 As Integer
Dim j1 As Integer
Dim k1 As Integer
Dim l1 As Integer
Dim iH As Integer
Dim iL As Integer
Dim lRow As Long
Dim Sh1 As Worksheet
Dim Sh2 As Worksheet
Set Sh1 = Worksheets("Sheet1")
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Solutions").Delete
On Error GoTo 0
Set Sh2 = Worksheets.Add(After:=Sh1)
Sh2.Name = "Solutions"
iH = Sh1.Range("A3").Value
iL = Sh1.Range("A3").Value / Sh1.Range("B3").Value
i1 = Sh1.Range("D2").Value
j1 = Sh1.Range("E2").Value
k1 = Sh1.Range("F2").Value
l1 = Sh1.Range("G2").Value
lRow = 0
i = i1
For j = j1 To iH - i Step j1
For k = k1 To iH - i - j Step k1
l = iH - i - j - k
If l Mod l1 = 0 Then
If i / i1 + j / j1 + k / k1 + l / l1 = iL Then
lRow = lRow + 1
With Sh2
.Cells(lRow, "A") = i
.Cells(lRow, "B") = j
.Cells(lRow, "C") = k
.Cells(lRow, "D") = l
End With
End If
End If
Next k
Next j
End Sub
Bookmarks