+ Reply to Thread
Results 1 to 6 of 6

VBA - Nested loops

  1. #1
    Registered User
    Join Date
    11-24-2009
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    8

    VBA - Nested loops

    Used to be able to work with VBA, but had a car accident about 13 months ago and haven't been able to concentrate since then. Very frustrating. Feel guilty asking for help but I've spent hours trying to figure out how to make VBA work for me and I just get so frustrated that I can't do it. Damn. (can I say that?) Anyway, I think these are pretty straight-forward for a VBA whiz. I sure do appreciate your taking a pass at these...

    I have a process in Excel that uses two independent variables, each of which is an integer between 3 and 40. Each combination of the two variables yields a different result and only one result is the largest. I need VBA code to sequentially step-through all combinations of the two variables to determine the largest result. I visualize a nested loop that checks the current result against the previous best and when the current result is larger, replaces the value in "BestResult" with the new value and also records the value of the variables at that time. Then, after completing the step-through, make Variable #1 and #2 equal the values that generated the largest result.

    The process resides on a sheet named "Engine". Variable #1 is in Cell G3. Variable #2 is in Cell M3. The current result is in Cell R3.

    Separate, unrelated project...

    I need VBA code to either increase or decrease Variable A until it triggers a change in Status. Status is currently either "On" or "Off" depending on the value of Var A. If it's currently "On", Var A can be decreased, one unit at a time, and eventually Status will change to "Off". Conversely, if Status is currently "Off", Var A it can be increased, one unit at a time, until Status will change to "On". I visualize a loop that would first check Status to determine whether to increase or reduce Var A, then make incremental changes to Var A until the exact number of units is found that will "flip" the Status, then stop.

    This process resides on a sheet named "Engine". Status is in Cell Q262. Variable A is in Cell C262, and may require a change of 1 to 1,000 units to trigger a change in Status.

    Did I forget anything? (Strong possibility.) I'll gladly answer any questions.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: VBA - Nested loops

    I suggest you attach a small sample workbook with a representative sample of data and desired results.

  3. #3
    Registered User
    Join Date
    11-24-2009
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: VBA - Nested loops

    Here's a sample workbook...
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: VBA - Nested loops

    Here's some code for your first question, which runs as you envisaged. Will look at your second question later.
    Please Login or Register  to view this content.
    Last edited by StephenR; 11-25-2009 at 01:00 PM.

  5. #5
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: VBA - Nested loops

    I think this works. Not sure what you want doing with the result so I just put it in a message box.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    11-24-2009
    Location
    Phoenix, Arizona
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: VBA - Nested loops

    Excellent, elegant solutions to both my problems. Apologize for much-delayed response. Have only recently regained access to Internet. Thank you much.

+ 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