Share via

Creating an Excel Matrix

Anonymous
2013-05-14T21:49:43+00:00

HI, guys

I have a problem at work that requires me to set up a matrix that will return all the possible combinations for serving a research meal restaurant style.  Here is the set up:

The meal will have several possible combinations. These are the choices:

Entree:  Chicken ($3.75); beef ($7.73); pork ($3.53)

Starch:  Potatoes ($0.85); rice pilaf ($2.21)

Soup:  minnestrone ($3.28); chicken noodle ($3.092)

Dessert:  cake ($0.60); berry crisp ($0.33); apple pie ($1.09)

Beverage:  wine ($2.80); water ($0.0)

Is there a formula in Excel that will return all possible combinations and costs for choosing one from each category to make a meal selection?  How would I set it up?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Anonymous
    2013-05-18T07:59:03+00:00

    Hi Tisino;

    I started to create a mockup that only allowed one selection for the first four and two selections for the second but I was using macros and realized somewhat belatedly that macro-enabled workbooks are not always well received in a general distribution.

    So I finished that one and converted the functionality over to another non-macro based workbook as best as I could using more complicated formulas and a fair bit of conditional formatting rules. In this macro-free workbook, multiple selections (e.g. X's) can be made but are not displayed and their prices are not recorded. If a second X is applied to the Entree group, only the first one (from the top down) is displayed. The second one remains an invisible X and if the first one is cleared then the second becomes visible and its pricing replaces the first.

    On both sheets, I've used named ranges for the five serving categories to tighten up code and formulas. Look in View, Name Manager for more details on these. These would also facilitate adding more meal items to the groups by adjusting automatically to an inserted row.

    I'll let you have a look and decide which is a better fit for your purpose. You will have to download them for full functionality.

    Reservation_v2 (CF rules no macros).xlsx

    Reservation_v2 (selection names and macros).xlsm

    1 person found this answer helpful.
    0 comments No comments

Answer accepted by question author

  1. Anonymous
    2013-05-15T03:34:55+00:00

    You are going to require a VBA routine that recursively loops through all combinations.

    I wrote the following sample routine and helper function to achieve the above.

    Sub mcr_Build_Meal()

    Dim e As Long, rw As Long, s As Long, sp As Long, d As Long, b As Long

    Dim p As Double, vr As Variant, vs As Variant, app As Application

    Set app = Application

    vs = Array("Entrée", "Starch", "Soup", "Dessert", "Beverage")

    vr = Array(vbNullString, vbNullString, vbNullString, vbNullString, vbNullString)

    With ActiveSheet

    .Cells(1, 5).CurrentRegion.Offset(1, 0).ClearContents

    For e = 1 To app.CountIf(.Columns(3), vs(0))

    vr(0) = fcnGet_Food_from_Serving_and_Pos(CStr(vs(0)), e)

    For s = 1 To app.CountIf(.Columns(3), vs(1))

    vr(1) = fcnGet_Food_from_Serving_and_Pos(CStr(vs(1)), s)

    For sp = 1 To app.CountIf(.Columns(3), vs(2))

    vr(2) = fcnGet_Food_from_Serving_and_Pos(CStr(vs(2)), sp)

    For d = 1 To app.CountIf(.Columns(3), vs(3))

    vr(3) = fcnGet_Food_from_Serving_and_Pos(CStr(vs(3)), d)

    For b = 1 To app.CountIf(.Columns(3), vs(4))

    vr(4) = fcnGet_Food_from_Serving_and_Pos(CStr(vs(4)), b)

    rw = .Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).Row

    .Cells(rw, 5).Resize(1, 5) = vr

    .Cells(rw, 10) = _

    app.SumIfs(.Columns(2), .Columns(1), .Cells(rw, 5).Value, .Columns(3), vs(0)) + _

    app.SumIfs(.Columns(2), .Columns(1), .Cells(rw, 6).Value, .Columns(3), vs(1)) + _

    app.SumIfs(.Columns(2), .Columns(1), .Cells(rw, 7).Value, .Columns(3), vs(2)) + _

    app.SumIfs(.Columns(2), .Columns(1), .Cells(rw, 8).Value, .Columns(3), vs(3)) + _

    app.SumIfs(.Columns(2), .Columns(1), .Cells(rw, 9).Value, .Columns(3), vs(4))

    Next b

    Next d

    Next sp

    Next s

    Next e

    End With

    Set app = Nothing

    End Sub

    Private Function fcnGet_Food_from_Serving_and_Pos(sFd As String, iPos As Long) As String

    Dim f As Long, tmp As String, fr As Range

    With ActiveSheet.Cells(1, 1).CurrentRegion

    Set fr = .Cells(1, 3)

    For f = 1 To iPos

    Set fr = .Columns(3).Find(What:=sFd, After:=fr, LookIn:=xlValues, LookAt:=xlPart)

    Next f

    fcnGet_Food_from_Serving_and_Pos = .Cells(fr.Row, 1).Value

    End With

    Set fr = Nothing

    End Function

    Post back if you have aany trouble transcribing that for your own purposes. I;ve uploaded the sample workbook to my SkyDrive here for your reference and download, Please note that my Excel autocorrected Entree to Entrée and I kept that in the code.

    Menu_Combinations.xlsm

    0 comments No comments

19 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-05-15T03:43:01+00:00

    Wow!

    Thank you Jeeped.  I knew there was a way to do it.  But, I thought it would be as simple as opening the "What if?" analysis wizard.  Does that wizard walk you through the same process or is writing the VBA to get to this the only way to end up with this result?  (I'm ore or less thinking out loud here!).

    Much appreciated.

    0 comments No comments
  2. Anonymous
    2013-05-15T03:13:54+00:00

    Hi, Ashish

    No, all combinations must include one of each item from each of the five categories.  So, no soup, water, wine and dessert only combination.  Thx.

    0 comments No comments
  3. Ashish Mathur 101.8K Reputation points Volunteer Moderator
    2013-05-15T02:12:13+00:00

    Hi,

    Returning all possible combinations would mean getting something like Chicken Noodle and Water as well.  Would you want such combinations as well.

    0 comments No comments