+ Reply to Thread
Results 1 to 10 of 10

replacing nested if statements with some kind of VBA loop?

  1. #1
    Registered User
    Join Date
    04-10-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    10

    replacing nested if statements with some kind of VBA loop?

    I have a worksheet where there are 16 column (each one has a student's major for a single semester).
    I want to be able to do the following:

    1) This part I have already, but it's an ugly brute force solution that can't be generalized to the future cases: For each row, pick the first column where the cell is not blank, and where it is different from the major listed in the first column and return both the column number of this column and its contents (the second major), or if this never happens return a blank cell. Here's how I did that:

    Ugly brute force formula to return second major if there ever was a major change:
    =IF(NOT((D2=0)+(D2=C2)),D2,IF(NOT((E2=0)+(E2=C2)),E2,IF(NOT((F2=0)+(F2=C2)),F2,IF(NOT((G2=0)+(G2=C2)),G2,IF(NOT((H2=0)+(H2=C2)),H2,IF(NOT((G2=0)+(G2=C2)),G2,IF(NOT((H2=0)+(H2=C2)),H2,IF(NOT((I2=0)+(I2=C2)),I2,IF(NOT((J2=0)+(J2=C2)),J2,IF(NOT((K2=0)+(K2=C2)),K2,IF(NOT((L2=0)+(L2=C2)),L2,IF(NOT((M2=0)+(M2=C2)),M2,IF(NOT((N2=0)+(N2=C2)),N2,IF(NOT((O2=0)+(O2=C2)),O2,IF(NOT((P2=0)+(P2=C2)),P2,IF(NOT((Q2=0)+(Q2=C2)),Q2,IF(NOT((R2=0)+(R2=C2)),R2,"")))))))))))))))))


    Formula to return the column in which the second major appears:
    {=IF(U2="","",MIN(IF(D2:R2=U2,D$1:R$1)))}

    I'll call this value T_1

    2) Repeat this process to find times when students changed their major a second time. This is tricky, though, because I can't used the nested if statement approach this time, since the place in the array where the nested if statement procedure should begin and end is dependent upon the semester in which the student first activated their second major. Logically, what I want is something like this to give me the third major that a student may have chosen:

    Let n=T_1+1 (where T_1 is the column number where the second major first appears)
    If the value in the cell in column n is not (blank or the same as the cell in column T_1), then return the text (major) in that cell, otherwise, let n=n+1 and repeat, for all n=T_1+1,T_1+2,...,16. If we get to n=17, terminate this loop and return a blank cell.

    And then similarly something like this for the column in which this third major first appeared:
    Let n=T_1+1 (where T_1 is the column number giving the first major that differs from their initial major)
    If the value in the cell in column n is not (blank or the same as the cell in column T_1), then return n, otherwise, let n=n+1 and repeat, for all n=T_1+1,T_1+2,...,16. If we get to n=17, terminate this loop and return a blank cell.

    I'll then need to repeat this process for the fourth new major and the fifth new major, etc, but once I have the code for the third new major, extending it to these cases would be obvious...

    I assume I can't do this with formulas in excel and will need to do some kind of VBA code, but I really don't have much experience in that, and attempts at googling for the past few days haven't really yielded anything useful. I was hoping someone could help me to construct the necessary code for this (and in the best case scenario help me understand how it works ). I'm a mathematician, so logic concepts are all really clear to me, but my programming experience is very spotty, and therefore I often don't know how to practically implement logical processes in a particular computer code...

    Thanks for taking the time to read my post!

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: replacing nested if statements with some kind of VBA loop?

    cwyn,

    Attached is an example workbook based on the criteria you described.
    In cell T3 and copied over and down to X6 is this array formula (note that array formulas must be entered with Ctrl+Shift+Enter and not just Enter):
    Please Login or Register  to view this content.

    Is something like that what you're looking for?
    Attached Files Attached Files
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    04-10-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    10

    Re: replacing nested if statements with some kind of VBA loop?

    Hi tigeravatar,
    Thanks so much for the help! This is really close, but doesn't quite do what I want, since often the major is listed for several semesters before it changes (and sometimes it's blank in between), for example, like this:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1st 2nd 3rd
    MAT MAT MAT NUR NUR SCI SCI MAT NUR SCI

    Is there a way to modify your array formula so that it also works in this case?
    Last edited by cwyn; 10-25-2012 at 03:18 PM.

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: replacing nested if statements with some kind of VBA loop?

    What happens if a major starts as PCN, changes to NUR, and then changes back to PCN?
    So that the changes would be PCN -> NUR -> PCN
    Or is that not a possible scenario?

  5. #5
    Registered User
    Join Date
    08-27-2012
    Location
    Omaha, NE
    MS-Off Ver
    Excel 2010
    Posts
    57

    Re: replacing nested if statements with some kind of VBA loop?

    That brute force equation gave me like 9 migraines trying to figure it out. So i'm gonna pass at attempting to solve this problem, but I'm interested in the solution.
    Piggy

  6. #6
    Registered User
    Join Date
    04-10-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    10

    Re: replacing nested if statements with some kind of VBA loop?

    Quote Originally Posted by tigeravatar View Post
    What happens if a major starts as PCN, changes to NUR, and then changes back to PCN?
    So that the changes would be PCN -> NUR -> PCN
    Or is that not a possible scenario?
    Yes, that can also happen, and in that case, I'd want the formula to return PCN as the first major, NUR as the second major, and then PCN as the third major...

  7. #7
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: replacing nested if statements with some kind of VBA loop?

    cwyn,

    Attached is version 2. It uses a UDF I created for this purpose. The UDF is named ListChange and takes the following two arguments:
    varData: This is a required argument that can be an array or range of cells. varData is used to determine where the list changes occur.
    [ChangeIndex]: This is an optional argument that specifies which list change you want returned. If omitted, the function will return the first list change (in your example, it would return MAT because that is the first item). The function treats this as a change from "nothing" to "something".

    If the function cannot determine a list change (either because there isn't one, or you requested a list change value with an invalid ChangeIndex parameter, for example you might have specified the 2nd argument as "a" which is invalid, or when there are only 3 changes and you specify a ChangeIndex of 4), it will return the #VALUE! error.

    In the attached .xlsm file in cell T3 and copied right is this formula that utilizes the UDF:
    Please Login or Register  to view this content.
    [EDIT]:
    Added a .xls version in the attachments section. In the .xls file, in cell T3 and copied right is this formula that utilizes the UDF:
    Please Login or Register  to view this content.

    Here is the code used for the UDF:
    Please Login or Register  to view this content.



    How to use a UDF:
    1. Make a copy of the workbook the UDF will be stored in
      • Always run new code on a workbook copy, just in case the code doesn't run smoothly
      • This is especially true of any code that deletes anything
    2. In the copied workbook, press ALT+F11 to open the Visual Basic Editor
    3. Insert | Module
    4. Copy the provided code and paste into the module
    5. Close the Visual Basic Editor
    6. The UDF is now available for use
    Last edited by tigeravatar; 10-25-2012 at 05:47 PM. Reason: Added a .xls version of the workbook and corresponding formula (for those that don't have Excel 2007+)

  8. #8
    Registered User
    Join Date
    04-10-2012
    Location
    New York City
    MS-Off Ver
    Excel 2007 and 2010
    Posts
    10

    Re: replacing nested if statements with some kind of VBA loop?

    tigeravatar, thank you so much - this totally solves my problem!
    I can't thank you enough!!

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: replacing nested if statements with some kind of VBA loop?

    You're very welcome

    If that takes care of your need, please mark this thread as solved.
    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:
    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  10. #10
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: replacing nested if statements with some kind of VBA loop?

    @ cwyn

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a relatively new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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