+ Reply to Thread
Results 1 to 6 of 6

Nested Loops

  1. #1
    Registered User
    Join Date
    08-24-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    37

    Nested Loops

    Hi all,

    I'm struggling with nesting a loop. I have a series of calculations that loops 5 times and offsets a row each time. I then want to repeat this series of loops X amount of times. The code below is only performing the whole task on one row and not the 5 I need.

    Please Login or Register  to view this content.
    Last edited by Walter12; 10-05-2012 at 09:06 AM.

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,423

    Re: Nested Loops

    Not a good idea to use row as the name of your variable - make it my_row or something. Anyway, you have the Dim statement outside the Sub.

    Have you tried stepping through with F8 and tracking what is happening?

    Hope this helps.

    Pete

  3. #3
    Registered User
    Join Date
    08-24-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Nested Loops

    Thanks Pete.
    Followed your tips but I think it's something I'm doing with selecting the rows.

    I'm on a Mac so F8 means nothing here! What does it do?

  4. #4
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,423

    Re: Nested Loops

    F8 allows you to single-step through the macro one statement at a time, and to examine the values of variables at each stage.

    Pete

  5. #5
    Registered User
    Join Date
    08-24-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Nested Loops

    I have an option to Step Into if that's the same thing?

    It's odd, if I swap the outer Do Loop While for a For Next loop that counts in the same way I have the same problem. All I need to do is to carry out the inner loop X amount of times. What my code does is carry out the inner loop once on all 5 rows 8 times on the first one (instead of all 5). Seems simple in principle, but I'm struggling to see where I'm going wrong.

  6. #6
    Registered User
    Join Date
    08-24-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Nested Loops

    I've managed to solve this using 2 For Next Loops instead. Here's the code if anyone else finds it of any use (as I have done to the solutions of many other threads). The basic principle is to loop a series of functions that are already looped. One use of this is for Monte Carlo type procedures where you loop a series of calculations to emulate a market phase. All calculations for the market are done per row, and this is repeated for each row using the inner calcLoop. This then needs to be queried a large number of times to test the rigidity of the market.

    Please Login or Register  to view this content.
    Last edited by Walter12; 10-06-2012 at 07:34 AM.

+ 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