+ Reply to Thread
Results 1 to 4 of 4

Combinations summing up to 100%

Hybrid View

  1. #1
    Registered User
    Join Date
    12-05-2007
    Posts
    16

    Combinations summing up to 100%

    Could someone explain me how I can get in excel all the combinations of 3 variables with in an interval of 1%.
    For example when there are 2 variables there are 11 combinations with an interval of 10%.

    1.0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0
    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

    Thanx in advance!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    This will list the 5151 combinations. It uses whole numbers rather than decimals. but you can divide all the numbers by 100.
    Sub testnComb()
        Debug.Print nComb(3, 100)
    End Sub
    
    Function nComb(nItems As Long, nTot As Long) As Long
        ' Returns the number of combinations that nItems can be adjusted
        ' in increments of 1 such that the total is always nTot
    
        Dim ai()    As Long
        Dim i       As Long
        Dim iSum    As Long
    
        ReDim ai(1 To nItems)
    
        ' setup to get first combination and ensure initial correct total
        ai(1) = -1
        ai(nItems) = nTot
        iSum = nTot - 1
    
        Do
            iSum = iSum - ai(nItems)
            ai(nItems) = 0
            For i = 1 To nItems - 1
                If iSum < nTot Then
                    iSum = iSum + 1
                    ai(i) = ai(i) + 1
                    Exit For
                Else
                    iSum = iSum - ai(i)
                    ai(i) = 0
                End If
            Next i
            If i > nItems - 1 Then Exit Function    '-------------------------->
            ai(nItems) = nTot - iSum
            iSum = nTot
            nComb = nComb + 1
            '*******************************
            Dim iRow As Long
            iRow = iRow + 1
            Cells(iRow, 1).Resize(, nItems).Value = ai
            '*******************************
        Loop
    End Function

  3. #3
    Registered User
    Join Date
    12-05-2007
    Posts
    16
    Thank you very much for providing me the code!

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You're welcome

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1