title: In Excel VBA, how could I compute sum of values which has a total limit that should not exceed $500, and then get corresponding product combination?
body: <p>I have a table with two Columns Product and Price($).</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">Product</th>
  <th style="text-align: right;">Price($)</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">A</td>
  <td style="text-align: right;">100</td>
  </tr>
  <tr>
  <td style="text-align: left;">B</td>
  <td style="text-align: right;">400</td>
  </tr>
  <tr>
  <td style="text-align: left;">C</td>
  <td style="text-align: right;">350</td>
  </tr>
  <tr>
  <td style="text-align: left;">D</td>
  <td style="text-align: right;">50</td>
  </tr>
  <tr>
  <td style="text-align: left;">E</td>
  <td style="text-align: right;">515</td>
  </tr>
  <tr>
  <td style="text-align: left;">F</td>
  <td style="text-align: right;">140</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I am trying to use vba to get combination of value of all products that will not exceed $500. I have been trying with this code and I am not sure how to proceed from this point on.</p>
  <pre><code>Sub getCombination()
      Dim price As Long
      Dim limit As Long
      Dim i As Integer
      Dim j As Integer
      Dim combination As String
          
      limit = 500
      combination = &quot;&quot;
      Range(&quot;B2&quot;).Activate
      price = Range(&quot;B2&quot;).Value
          
      For i = 1 To 6
          For j = 1 To 6
              If price &lt;= limit Then
                  price = price + ActiveCell.Offset(j, 0).Value
                  combination = combination &amp; ActiveCell.Offset(0, -1).Value &amp; &quot;,&quot; &amp; ActiveCell.Offset(1, -1).Value
              End If
          Next j
      Next i
      
      ActiveCell.Offset(1, 0).Activate
      MsgBox combination
  
  End Sub
  </code></pre>
  <p>My Expected output is something like<br />
  A,B<br />
  A,C<br />
  A,C,D<br />
  B,D<br />
  C,F<br />
  A,D<br />
  C,D
  (Please note: Not All output combinations are specified here!)</p>
  <p>How should I proceed with the existing code? Or do I really have a better way for me to implement this?</p>