Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Determining Combinations to Make a Total.

Determining Combinations to Make a Total

Written by Allen Wyatt (last updated April 15, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365


2

Suppose you have a worksheet with three columns of data. The first column has in sequential order each letter of the alphabet, A through Z. The second column contains a number of occurrences that correlates with the letter in the alphabet. The third column contains a number of hours that correlates with the letter in the alphabet.

What if you want to distribute, as evenly as possible, a combination of the alphabet letters into four groups based on the third column (hours)? For example, if the sum of all the hours for each letter of the alphabet is 4,000 hours, you want to come up with a combination that would segregate the alphabet so that each one of the four groups would have around 1,000 hours per group.

This is actually a well-known problem in the field of discrete mathematics. A variety of algorithms have been developed to provide solutions, and there are certain programming languages (such as LISP) that greatly facilitate creating tree structures that can "search" for optimal solutions.

In this case, however, a simple approach is best, and that involves using a macro. Let's assume that you have your data in columns A through C. The following macro will analyze the range you specify and return a combination of values that fulfill your requirements.

Function DoDist(sRaw As Range, _
  iTCol As Integer, _
  iBuckets As Integer, _
  iWanted As Integer, _
  iRetCol As Integer) As String

    Dim lGTotal As Long
    Dim lPerBucket As Long
    Dim lCells() As Long
    Dim sRet() As String
    Dim lBk() As Long
    Dim sBk() As String
    Dim lTemp As Long
    Dim sTemp As String
    Dim J As Integer
    Dim K As Integer
    Dim L As Integer

    Application.Volatile
    ReDim lCells(sRaw.Rows.Count)
    ReDim sRet(sRaw.Rows.Count)
    ReDim lBk(iBuckets)
    ReDim sBk(iBuckets)

    lGTotal = 0
    For J = 1 To sRaw.Rows.Count
        lCells(J) = sRaw(J, iTCol)
        lGTotal = lGTotal + lCells(J)
        sRet(J) = sRaw(J, iRetCol)
    Next J

    For J = 1 To sRaw.Rows.Count - 1
        For K = J + 1 To sRaw.Rows.Count
            If lCells(J) < lCells(K) Then
                lTemp = lCells(J)
                lCells(J) = lCells(K)
                lCells(K) = lTemp
                sTemp = sRet(J)
                sRet(J) = sRet(K)
                sRet(K) = sTemp
            End If
        Next K
    Next J

    lPerBucket = lGTotal / iBuckets
    For J = 1 To sRaw.Rows.Count
        L = iBuckets
        For K = iBuckets To 1 Step -1
            If lBk(K) <= lBk(L) Then L = K
        Next K
        lBk(L) = lBk(L) + lCells(J)
        sBk(L) = sBk(L) & sRet(J) & ", "
    Next J

    For J = 1 To iBuckets
        If Right(sBk(J), 2) = ", " Then
            sBk(J) = Left(sBk(J), Len(sBk(J)) - 2)
        End If
        sBk(J) = sBk(J) & " (" & lBk(J) & ")"
    Next J

    DoDist = sBk(iWanted)
End Function

Notice that this function is passed five parameters. The first is the range that you want evaluated, the second is the offset of the column within that range that should be totaled, the third is the number of "buckets" you want to use in the evaluation, the fourth is the number of the bucket that you want to return, and the fifth is the offset of the column (in the specified range) that contains the values you want returned.

What the macro does is to grab all the values in the column you want totaled, and then sort them in descending order. These values, from largest to smallest, are then distributed among however many "buckets" you specified that there should be. The number is always added to the bucket that contains the smallest total. The string that is returned by the function represents the return values (whatever is in each cell of the column specified by the fifth parameter) and the total of the bucket.

For instance, if you wanted to evaluate the range A1:C:26, you wanted the distribution to be based on the values in the third column of the range (column C), you wanted there to be four buckets in the analysis, you wanted the third bucket returned, and you wanted to have the function return whatever is in column A of the range, then you would use the following to call the function:

=DoDist(A1:C26,3,4,3,1)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12234) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Determining Combinations to Make a Total.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing without Opening

Want to print one or more workbooks without the need of actually opening the file? It's easy to do when you rely on ...

Discover More

Converting Text Inside Double Asterisks to Bold

In plain-text documents, it is not uncommon to see asterisks used around text to indicate what should be considered bold ...

Discover More

Printing A4 on Letter Size Paper

For those around the world using the metric system, the standard size for many documents is referred to as an A4 page. If ...

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Saving Common Formulas

Got some formulas you slaved over and want to use in lots of workbooks? This tip presents some helpful ideas on how you ...

Discover More

Adjusting Formulas for Top-Added Rows

Formulas are the heart of using Excel, and formulas often refer to ranges of cells. How you insert cells into the ...

Discover More

Relative References within Named Ranges

Excel is usually more flexible in what you can reference in formulas than is immediately apparent. This tip examines some ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 3 + 2?

2023-04-18 09:36:07

J. Woolley

@Dave S
Your version is correct in a VBA statement.
The Tip's version is correct in a cell formula.


2023-04-17 05:34:16

Dave S

I think the call statement should to be

=DoDist(Range("A1:C26"),3,4,3,1)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.