+ Reply to Thread
Results 1 to 6 of 6

Dynamic variable state combinations

  1. #1
    Registered User
    Join Date
    04-10-2007
    Posts
    34

    Exclamation Dynamic variable state combinations

    Hi,

    I'm new to visual basic and have been using Excel for some time. I have searched the forums for a solution to my problem and the closest I have is from http://www.excelforum.com/showthread...t=combinations however... I don't understand the solution.

    My Problem:

    On one sheet (input) I ask a user to input the number of variables to be considered (e.g. 2 variables). On another sheet (States) I read in the number of variables and ask the user to input a number of states for each variable (e.g. variable 1 has x states, variable 2 has y states). Once the user inputs the number of states I then ask then user to input the names of each state for each variable (e.g. State 1 for variable 1 = True, State 2 for variable 1 = False, State 1 for variable 2 = True, State 2 for variable 2 = False). The number of variables and the number of states can vary.

    I have managed to create VB macros to this point and now I am stuck. I would like read in the states and create a table with all possible combinations in the cells. E.g. if there are 2 variables there will be two columns and, using the example above, the table will look like:

    Variable 1 Variable 2
    True True
    True False
    False True
    False False

    If there are 3 binary true/false variables the table becomes:

    Variable 1 Variable 2 Variable 3
    True True True
    True True False
    True False True
    True Flase False
    False True True
    False True False
    False False True
    False False True

    The state inputs always start in cell E1 in the states sheet with a space after each variable state list. E.g. if variable 1 has 2 states they will be listed in E1 and E2, Variable 2 states will start in E4.

    Can anyone help? I hope I have provided enough information.

    Mbrolass.
    Last edited by Mbrolass; 04-18-2007 at 12:14 PM.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    This is an interesting problem and it lends itself to a recursive programming approach.

    The code below uses a couple of procedures (ProcessArray and AddCodes) which call each other repeatedly while an array of possible combinations reduces in size until it is only one variable big. At this point, the line of values is written to the spreadsheet.

    To keep things simpler for me, I’ve hard coded a set of variables (V1 to V5) and their possible states (S1 – Sn) . In the example, variable 1 has 4 states, variable 2 has 3, variable 3 has 4, variable 4 has 4 and finally variable 5 has five. This should result in 4x3x4x4x5 = 960 combinations.

    You will need to adapt the code to fit in with the plans that you have to ask the user to define the number of variables and their possible states and get the positioning on the sheet the way that you want it.

    I hope that this is useful.



    Please Login or Register  to view this content.
    Martin

  3. #3
    Registered User
    Join Date
    04-10-2007
    Posts
    34

    Smile

    Thanks Martin,

    This is absolutely brilliant... I think it would have taken me about a year to figure this out alone. You are a star!!!

  4. #4
    Registered User
    Join Date
    04-10-2007
    Posts
    34
    Martin,

    I can't thank you enough for this program. I wonder if I can ask you for one more favour... in your current program variable 5 has the largest number of states. I tried running the same program, slightly modified to have 2, 4 and 3 states for 3 variables only. In this example I get the following output:

    V1S1 V2S1 V3S1
    V1S1 V2S1 V3S2
    V1S1 V2S1 V3S3
    V1S1 V2S1
    V1S1 V2S2 V3S1
    V1S1 V2S2 V3S2
    V1S1 V2S2 V3S3
    V1S1 V2S2
    V1S1 V2S3 V3S1
    V1S1 V2S3 V3S2
    V1S1 V2S3 V3S3
    V1S1 V2S3
    V1S1 V2S4 V3S1
    V1S1 V2S4 V3S2
    V1S1 V2S4 V3S3
    V1S1 V2S4
    V1S2 V2S1 V3S1
    V1S2 V2S1 V3S2
    V1S2 V2S1 V3S3
    V1S2 V2S1
    V1S2 V2S2 V3S1
    V1S2 V2S2 V3S2
    V1S2 V2S2 V3S3
    V1S2 V2S2
    V1S2 V2S3 V3S1
    V1S2 V2S3 V3S2
    V1S2 V2S3 V3S3
    V1S2 V2S3
    V1S2 V2S4 V3S1
    V1S2 V2S4 V3S2
    V1S2 V2S4 V3S3
    V1S2 V2S4

    I believe this is because the MaxStates variable is set to 4 (which is the largest number of states in any variable). How can the code be altered so that it doesn't matter which variable has the largest number of states? The output shouldn't contain blank cells.

    Thanks again.

    Mbrolass.

  5. #5
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967
    Opps - sorry!

    The following altered version of the AddCodes procedure should help. It checks that the last value is not a blank before writing the rest

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    04-10-2007
    Posts
    34

    Talking Thanks

    Works perfectly now. Thanks for all your help.

+ 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