+ Reply to Thread
Results 1 to 6 of 6

Automatically determine If statement from multiple conditions selected?

  1. #1
    Registered User
    Join Date
    11-01-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    9

    Automatically determine If statement from multiple conditions selected?

    Hi All,

    I’m trying to code an automated trading system that will perform a trade if certain conditions are met. Assume there are 5 conditions, and the user can select any one, two, three, four or five of the conditions together. This means the user could select any number of conditions, and any possible permutation of conditions of that size. Hence, there are dozens of distinct possible choices.

    In my code, each condition is being determined by a function, where the function will return a true or false value. Let’s call them FunctionCondition1, FunctionCondition2, FunctionCondition3….etc.

    For example, if the user selects condition 1 and condition 3, the vba would be as follows:

    Please Login or Register  to view this content.
    If the user selects conditions 3,4 and 5, the vba would be:

    Please Login or Register  to view this content.
    I’m wondering if it’s possible to code something that will be able to automatically construct or use an If statement (depending on the selected conditions) without having to list all the possible combinations of conditions in If statements. Therefore, at the start of the code it would pick up (using simple Booleans) how many and which conditions have been selected by the user, and from that it would be able to construct (or work out) the appropriate If statement to use.

    Any ideas?

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Automatically determine If statement from multiple conditions selected?

    Hi Marco123, is there any chance you can upload a mock workbook with mock data in it? It take the guess work away from those that will help.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    11-01-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Automatically determine If statement from multiple conditions selected?

    Hi Mordred, after posting on another forum I was given the solution. It is actually very simple and uses a combination of boolean variables. See below:

    bTrade=true
    If bMyCondition1 then bTrade = bFunctionCondition1
    If bMyCondition2 then bTrade = bTrade and bFunctionCondition2
    If bMyCondition3 then bTrade = bTrade and bFunctionCondition3

    If bTrade then 'perform a trade

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Automatically determine If statement from multiple conditions selected?

    One way is to make a unique string for each combination of condition2

    Please Login or Register  to view this content.
    Note that Result looks like a binary number. This version returns those numbers,

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    11-01-2011
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Automatically determine If statement from multiple conditions selected?

    Hi mikerickson, thanks for your answer but I'm not sure if it works. It still seems that I would have to list all possible permutations in the case statements. Have I missed anything?

    P.S. I have uploaded a mock workbook which has a solution in it that works.
    Attached Files Attached Files

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Automatically determine If statement from multiple conditions selected?

    I'm not sure what the question is. If you have 5 boolean values, there are 32 possibilities for their various combinations.
    If each of those combinations is to be treated differently, your code will have to have a 32 way branch, of one form or another.

    If statements are one way to achieve that branching. The SelectCase is another. I find Select case to be easier to read than If..ElseIf...ElseIf...End constructions, but there still has to be 32 branches.

+ 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