Sub Knapsack()
Dim limit As Double, weight As Double, value As Double, totalWeight As Double, maximumValue As Double
Dim g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z As Integer
'Limit of Weight
limit = Range("D7").value
'Weights
peso1 = Range("B2").value
peso2 = Range("C2").value
peso3 = Range("D2").value
peso4 = Range("E2").value
peso5 = Range("F2").value
peso6 = Range("G2").value
peso7 = Range("H2").value
peso8 = Range("I2").value
peso9 = Range("J2").value
peso10 = Range("K2").value
peso11 = Range("L2").value
peso12 = Range("M2").value
peso13 = Range("N2").value
peso14 = Range("O2").value
peso15 = Range("P2").value
peso16 = Range("Q2").value
peso17 = Range("R2").value
peso18 = Range("S2").value
peso19 = Range("T2").value
peso20 = Range("U2").value
'Values
valor1 = Range("B3").value
valor2 = Range("C3").value
valor3 = Range("D3").value
valor4 = Range("E3").value
valor5 = Range("F3").value
valor6 = Range("G3").value
valor7 = Range("H3").value
valor8 = Range("I3").value
valor9 = Range("J3").value
valor10 = Range("K3").value
valor11 = Range("L3").value
valor12 = Range("M3").value
valor13 = Range("N3").value
valor14 = Range("O3").value
valor15 = Range("P3").value
valor16 = Range("Q3").value
valor17 = Range("R3").value
valor18 = Range("S3").value
valor19 = Range("T3").value
valor20 = Range("U3").value
'Quantities
cant1 = Range("B4").value
cant2 = Range("C4").value
cant3 = Range("D4").value
cant4 = Range("E4").value
cant5 = Range("F4").value
cant6 = Range("G4").value
cant7 = Range("H4").value
cant8 = Range("I4").value
cant9 = Range("J4").value
cant10 = Range("K4").value
cant11 = Range("L4").value
cant12 = Range("M4").value
cant13 = Range("N4").value
cant14 = Range("O4").value
cant15 = Range("P4").value
cant16 = Range("Q4").value
cant17 = Range("R4").value
cant18 = Range("S4").value
cant19 = Range("T4").value
cant20 = Range("U4").value
'Cálculo
maximumValue = 0
For g = 0 To cant1
For h = 0 To cant2
For i = 0 To cant3
For j = 0 To cant4
For k = 0 To cant5
For l = 0 To cant6
For m = 0 To cant7
For n = 0 To cant8
For o = 0 To cant9
For p = 0 To cant10
For q = 0 To cant11
For r = 0 To cant12
For s = 0 To cant13
For t = 0 To cant14
For u = 0 To cant15
For v = 0 To cant16
For w = 0 To cant17
For x = 0 To cant18
For y = 0 To cant19
For z = 0 To cant20
weight = peso1 * g + peso2 * h + peso3 * i + peso4 * j + peso5 * k + peso6 * l + peso7 * m + peso8 * n + peso9 * o + peso10 * p + peso11 * q + peso12 * r + peso13 * s + peso14 * t + peso15 * u + peso16 * v + peso17 * w + peso18 * x + peso19 * y + peso20 * z
value = valor1 * g + valor2 * h + valor3 * i + valor4 * j + valor5 * k + valor6 * l + valor7 * m + valor8 * n + valor9 * o + valor10 * p + valor11 * q + valor12 * r + valor13 * s + valor14 * t + valor15 * u + valor16 * v + valor17 * w + valor18 * x + valor19 * y + valor20 * z
If value > maximumValue And weight <= limit Then
Range("B5").value = g
Range("C5").value = h
Range("D5").value = i
Range("E5").value = j
Range("F5").value = k
Range("G5").value = l
Range("H5").value = m
Range("I5").value = n
Range("J5").value = o
Range("K5").value = p
Range("L5").value = q
Range("M5").value = r
Range("N5").value = s
Range("O5").value = t
Range("P5").value = u
Range("Q5").value = v
Range("R5").value = w
Range("S5").value = x
Range("T5").value = y
Range("U5").value = z
totalWeight = weight
maximumValue = value
End If
Next z
Next y
Next x
Next w
Next v
Next u
Next t
Next s
Next r
Next q
Next p
Next o
Next n
Next m
Next l
Next k
Next j
Next i
Next h
Next g
Range("B7").value = totalWeight
Range("B9").value = maximumValue
End Sub
Agrego este porque no pude agregarlo antes.