Nested Loops Quit after one successful round

  • Apologies if the thread name is vague, it's the best description I could come up with.


    Here's the issue:


    I have several loops of code nested together. Their purpose is to transpose the values of cells that are arranged horizontally in one worksheet (FinanceData) into a vertical format in a second worksheet (UtilData). In transposing the data, every account number (UCAC) has 12 rows in the new workbook (UtilData) to correspond to 12 columns in the original workbook (FinanceData). You may have already guessed that the 12 rows / columns correspond to calendar months and the data captures performance within those months. I use both the 'current' account number (CurrentUCAC) and the calendar month (SrchCell) as variables to identify the correct copy/paste cells between workbooks.



  • Re: Nested Loops Quit after one successful round


    I don't know exactly what is wrong with your code, but a few suggestions.
    I would suggest using long instead of integer when dealing with rows and columns.
    Activating workbooks and sheets is unnecessary and slows your code down.
    It may also be what is wrong, maybe not, but I would avoid it.
    I would suggest using variables for workbooks and sheets.
    For example instead of:

    Code
    Dim wbName as String
    wbName = ActiveWorkbook.Name
    Workbook(wbName).Activate


    Use

    Code
    Dim wb as Workbook
    Set wb = ActiveWorkbook
    With wb
      ' Do something here, no need to activate.
    End With


    I personally like to use Next x or whatever the next refers to as opposed to just Next.
    It makes it easier to see what the Next line is referring to.


    Now onto your code, each time you see something like:

    Code
    Cells(x, y).Copy
    Cells(w, 11).PasteSpecial (xlPasteValues)


    Cells will always refer to the sheet that is active at the time the line of code is running.


    Is the sheet you want to be active when these lines of code run the sheet you want to be the active sheet?


    This is a big reason to avoid using activate and refer explicitly to a workbook and a worksheet, I would suggest using variables and with statements.

    Bruce :cool:

  • Re: Nested Loops Quit after one successful round


    I think this might be because you activated a new wb (FinanceData) at the end of your loop, so when it moves to the next item in the loop your code is focusing on (FinanceData) and not (UtilData).


    Try this:

  • Re: Nested Loops Quit after one successful round


    Thanks Max, but the 'search' code should be taking place in the Finance document (to find the data for transposing) before switching over to the new UtilData document.



    Skywriter: thanks for the advice, the 'with' method does look like a better method to use. Unfortunately I've already spent more time on this code than I wanted, this portion is just a small part of a larger macro. If I switched methods, I would have to restructure and rewrite most of the macro because it switches between both documents so much. Does the 'activate' method really cause such a massive memory drain that it can prevent code from completing loops?


    I will try switching some of the variables from integers to longs, and I'll clean up some of the 'next' statements to clarify them. Maybe something will click into place. Stay tuned!


    W

  • Re: Nested Loops Quit after one successful round


    Quote from Winoria;773159


    I will try switching some of the variables from integers to longs, and I'll clean up some of the 'next' statements to clarify them. Maybe something will click into place. Stay tuned!


    W


    Short answer is 'nope'. No change.


    Is switching from the '.activate' method to the 'with' method going to make a huge difference in program resource use? I've written a number of macros that switch between workbooks and worksheets using these methods and I've never had this specific problem before. The copy / paste seems to be working fine if the loops run through to the finish.


    So for example, if I insert breakpoints and watch the code step-by-step, it works like a champ. But when I remove those breakpoints, it trips up somewhere and ends the sub. No error messages, just acts like running through one round of 'y' variable is enough and done. Even 'y' shows '9' in the locals, though the code clearly stipulates '7 to 19'. For clarity, '8' is the first relevant column where the code ought to kick in.

  • Re: Nested Loops Quit after one successful round


    Look at the following code:


    I switch worksheets during the very first loop, therefore Cells the very first time the loop runs refers to sheet1, from the second time the loop runs until it ends cells refers to sheet2.


    I therefore end up with 1 number on sheet1 and the rest end up on sheet2.


    Now keeping this in mind look at your code and read my notes.

    Bruce :cool:

  • Re: Nested Loops Quit after one successful round


    OK I gotcha! In my mind this is a nesting problem, specifically that the code switching between workbooks were nested incorrectly. Next time I'll use the "with" method to designate workbooks and switch between them. Should avoid this problem all together.


    I updated the code (see below) and got one successful run! I'll cross my fingers that it wasn't just luck. Thanks for your help!


  • Re: Nested Loops Quit after one successful round


    The problem with activating workbooks/sheets isn't to do with memory drain, activating just isn't needed and slows things down.


    It also makes it hard to debug things as it can get kind of confusing as to what's actually active when.


    Anyway, here's your code without any activating.

    Boo!:yikes:

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!