[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VB Difficult Summation/Iteration Question

Posted on 2004-10-21
3
Medium Priority
?
189 Views
Last Modified: 2006-11-17
If I had 100 cells A1 through A100, how do I do a loop to select all possible combinations of 3 of the 100 cells.  And then how do I have 3 variables that sum to 100 so that they also loop through all combinations (integers only).

So z = v1 * c1 + v2 * c2 + v3 * c3 for each combination of c1,c2,c3 in those 100 cells and each combination of v1, v2, v3 for each integer combination of their sum to 100.

Thanks so much.
0
Comment
Question by:bcsmess
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 12378365
I can give you the code, but I can't give you the time to run it!

Sub CalculateCombos()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim v1 As Integer
Dim v2 As Integer
Dim v3 As Integer
Dim c1 As Integer
Dim c2 As Integer
Dim c3 As Integer
Dim total As Long
Const maxv = 100

With ActiveWorkbook.ActiveSheet
    For i = 1 To 98
        c1 = .Cells(i, 1)
        For j = i + 1 To 99
            c2 = .Cells(j, 1)
            For k = j + 1 To 100
                c3 = .Cells(k, 1)
                For v1 = 1 To maxv
                    For v2 = 1 To maxv
                        For v3 = 1 To maxv
                            total = v1 * c1 + v2 * c2 + v3 * c3
                        Next v3
                    Next v2
                Next v1
            Next k
        Next j
    Next i
End With
End Sub
0
 
LVL 4

Expert Comment

by:AjithJose
ID: 12394424
This is a problem of permutations, the formula is

nCr=n!/((n-r)!/r!)

In this case: 167000 unique combinations of cells are available

For sample code visit these urls:
http://www.vbexplorer.com/VBExplorer/files/permutation.zip

http://www.codeguru.com/vb/gen/vb_misc/algorithms/article.php/c5607/
http://www.vb-helper.com/howto_permute.html

~ AjithJose
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12394582
AjithJose,

That's just the cells.
The questioner also wants to use a multiplier (maximum unspecified) for each cell, so I guess there are actually
167000* the maximum multiplier value raised to the power of three.
In my code I have assumed that the maximum will be 100, so there would be 167,000,000,000 possibilities.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question