+ Reply to Thread
Results 1 to 9 of 9

2 Loops. One loop within another.

  1. #1
    Registered User
    Join Date
    02-20-2009
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    92

    2 Loops. One loop within another.

    Hi, I'm mostly a VBA amateur. Loops are my particular weakness. Sort of a one trick pony.

    I am trying to create a loop within an existing loop. One workbook has several sheets, lets say A,B,C, and D. I'd like to create a loop that starts with the first row in sheet A and loops through sheet B to find a match. If a match is found, data is added to sheet D. If no match is found or after the data is added to sheet D, I'd like to move to the row in sheet A and repeat until sheet A is a blank row.

    Thank you for the help. I really have trouble with loops combined with if statements. I really do appreciate the help I get on this site.

    Please Login or Register  to view this content.
    Last edited by starcraftbud; 03-22-2013 at 03:57 PM. Reason: Solved

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

    Re: 2 Loops. One loop within another.

    This is how I would approach that, instead of looping through rangeB, I use the FIND command to jump straight to it. If these are dates you're searching for, you might need to edit the macro to use xlPart instead of xlWhole in the FIND command.
    Please Login or Register  to view this content.
    _________________
    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!)

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

    Re: 2 Loops. One loop within another.

    Did you try it?

  4. #4
    Registered User
    Join Date
    02-20-2009
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    92

    Re: 2 Loops. One loop within another.

    I've been trying to get it to work. I think conceptually I understand it and it makes sense. I mistakenly left out some code that is giving me trouble. The trouble is with this line:
    Please Login or Register  to view this content.
    I actually want to match up multiple values in Sheet A to values in Sheet B. So I need to do something more like this for that line (but we changed the ranges so its not working the same):
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-20-2009
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    92

    Re: 2 Loops. One loop within another.

    But thanks for the help so far and for replying. I am going to play with it a bit more and if I figure it out, I'll be sure to post so not to waste anyone's time. Thanks again!

  6. #6
    Registered User
    Join Date
    02-20-2009
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    92

    Re: 2 Loops. One loop within another.

    I'm still having trouble getting this to work. Anyone have any ideas?

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: 2 Loops. One loop within another.

    Hello starcraftbud,

    When all else fails, post your workbook for review.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  8. #8
    Registered User
    Join Date
    02-20-2009
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    92

    Re: 2 Loops. One loop within another.

    Thank you Leith Ross. I have posted the workbook. Tabs A, B, and D are what the code references. The goal is to have sheet D tie to sheet Final D after the macro has completed running.

    On sheet B, I have highlighted the row which should match and trigger values moving to sheet D. Thank you for taking a look at this.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    02-20-2009
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    92

    Re: 2 Loops. One loop within another.

    I figured it out.

    I ended up doing this:
    Please Login or Register  to view this content.
    Thanks Jbeaucaire for giving me at least an idea to make it work!

+ 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