+ Reply to Thread
Results 1 to 8 of 8

For each loop nested; getting stuck on one cell in first iteration of nested loop

  1. #1
    Forum Contributor
    Join Date
    12-15-2009
    Location
    Herndon, VA
    MS-Off Ver
    Excel 2010
    Posts
    163

    Thumbs down For each loop nested; getting stuck on one cell in first iteration of nested loop

    Imagine the following:

    Schedule I
    A6 = 8006-001
    A7 = 9080-002
    A8 = xxxx
    etc.

    Schedule K - 2008 Final
    D12 = 8009-001
    B31 = TOTAL COSTS
    I31 = $9,000


    The above portion in bold on Schedule K repeats but the different values in the first and last cells. The middle cell will always say 'TOTAL COSTS.' This is where I am getting stuck on.

    In short, I am starting with cell A6 on Schedule I (this is the 'project' object variable in the first for each loop), taking that cell and going to Schedule K and running it down column D (using 'b' as the object variable for the second for each loop) to compare for a match. If a match is found inside of this second/nested for each loop, then I want to use the FIND method and move over 2 columns to the left, and then down that column (B) to locate the cell that contains the next occurence of the letters 'TOTAL COSTS.' Then from 'TOTAL COSTS' I want to move over 7 cells, get the value in that cell, and drop it in a cell on the first tab, Schedule I.

    The problem is that on the first iteration of the nested for each loop, the program is getting stuck on the object variable 'findTotalCost.' Then comes across and extracts $9,000 and pastes it on the Schedule I. This is ok for the first iteration. However the $9,000 value gets pasted again in the next cell below, again and again with each iteration.

    So the program is stuck on the first occurence of 'TOTAL COSTS.' I need to somehow clear the value of 'TOTAL COSTS' at the of each iteration. Al least that's what I think. That way each occurence of 'TOTAL COSTS' will be picked up not just the first one.

    Please help.



    Please Login or Register  to view this content.

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

    Re: For each loop nested; getting stuck on one cell in first iteration of nested loop

    Rather than asking us to imagine, please post a small sample workbook.

  3. #3
    Forum Contributor
    Join Date
    12-15-2009
    Location
    Herndon, VA
    MS-Off Ver
    Excel 2010
    Posts
    163

    Talking Re: For each loop nested; getting stuck on one cell in first iteration of nested loop

    First of all it's not that hard to code in a few values. I'm even telling you what cells to use. Secondly, if you can't "imagine" this, then you probably can't help much at all anyway.

  4. #4
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: For each loop nested; getting stuck on one cell in first iteration of nested loop

    Quote Originally Posted by Excel_vba View Post
    First of all it's not that hard to code in a few values. I'm even telling you what cells to use. Secondly, if you can't "imagine" this, then you probably can't help much at all anyway.
    Any reason why someone must try to imagine something instead of helping you? You are the one who needs help, make it easy for those who can help you.
    If you are pleased with a member's answer then use the Star icon to rate it.

  5. #5
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: For each loop nested; getting stuck on one cell in first iteration of nested loop

    Quote Originally Posted by Excel_vba View Post
    First of all it's not that hard to code in a few values. I'm even telling you what cells to use. Secondly, if you can't "imagine" this, then you probably can't help much at all anyway.
    You're asking for help, someone is asking you to put a little effort in yourself.

    I've read your description a couple of times and it's not the easiest to follow.

    To be fair I'm disinclined to pursue this any further after reading the above though.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

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

    Re: For each loop nested; getting stuck on one cell in first iteration of nested loop

    Excel_vba: clearly we've all misunderstood. There were all us thinking we were doing you a favour when actually it's the other way round.

    I think you need to change your attitude.

  7. #7
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: For each loop nested; getting stuck on one cell in first iteration of nested loop

    First of all it's not that hard to code in a few values. I'm even telling you what cells to use. Secondly, if you can't "imagine" this, then you probably can't help much at all anyway.
    Here's a clue: we ask for sample workbooks because . .

    1. We get to see the actual structure
    2. You can show the exact results you want and it removes ambiguity due to insufficient clarity in the post
    3. It eliminates a lot of questions
    4. You get a precise answer much faster

    And there are more reasons, but this is sufficient to paint you a picture. If you don't like the *FREE" help here . . . call Microsoft.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: For each loop nested; getting stuck on one cell in first iteration of nested loop

    Quote Originally Posted by Excel_vba View Post
    First of all it's not that hard to code in a few values. I'm even telling you what cells to use. Secondly, if you can't "imagine" this, then you probably can't help much at all anyway.
    This was really stupid thing to say, I hope you see that by now. On a forum that gives as much free feedback as this one, I'd expect a sincere apology is in order, yes?

    ========
    Keeping with the "help yourself" thread, I'll give you this and you can go try and apply it yourself. The problem is in this line of code:
    Please Login or Register  to view this content.
    The .FIND function has a LOT more parameters to control/resolve than just "what:=". By excluding them you force Excel to make assumptions. Read up some more on .FIND and fill in the missing parameters, specifically the "After:=" and your results will fall into line.



    ========
    EXCERPT FROM THE FORUM RULES:


    "If you want help with Excel, post an Excel file..."



    ...I'm just saying...
    Last edited by JBeaucaire; 12-22-2009 at 11:59 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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